Published at: 06:11 pm - Friday November 27 2009
在一台赤裸裸的RHEL4上部署web.py程序,一切从几乎是从零开始。以下操作均以root用户操作。 1. 安装MySQL数据库 下载安装MySQL wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.41-linux-i686-glibc23.tar.gz/from/http://mirror.services.wisc.edu/mysql/ 解压,按照INSTALL文件说明进行安装,不多赘述 2. 安装Python环境 下载Python源码 wget http://www.python.org/ftp/python/2.6.4/Python-2.6.4.tar.bz2 解压,编译安装,不需要特殊操作。 下载Easy_install wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg 安装 sh setuptools-0.6c11-py2.6.egg 安装相关Packages easy_install DBUtils easy_install flup easy_install web.py 安装mysql-python mysql-python包不能用easy_install安装,手动下载 wget http://downloads.sourceforge.net/project/mysql-python/mysql-python-test/1.2.3b1/MySQL-python-1.2.3b1.tar.gz?use_mirror=softlayer 加压,编辑site.cfg 指定mysql_config的路径,注意是新安装的mysql路径 mysql_config=/usr/local/mysql/bin/mysql_config 编译、安装 python setup.py build python setup.py install 声明libmysqlclient路径 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/lib 3. 安装服务器环境 下载安装fastcgi头文件 wget http://www.fastcgi.com/dist/fcgi.tar.gz 解压,默认编译安装 下载安装PCRE wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.00.tar.bz2 解压,默认编译安装 下载lighttpd wget [...]
Published at: 04:11 pm - Sunday November 22 2009
The Exaile plugin “Douban Covers” has been upgraded to 0.0.2. Now there is a new preference pane inside exaile preference dialog. This update provides you an optional choice to specify your own apikey when access douban.com open api. With an apikey, your request frequency will be raised to 40 times per minute. Again, you can [...]
Published at: 05:11 pm - Thursday November 12 2009
问题: 您要通过python的subprocess模块执行一些命令,要在程序中获得命令的标准输出,此外,命令中还有管道操作。 解决: 以这个命令为例: cat /home/admin/deploy/log/2009-11-11.log |grep example.com |wc -l import subprocess cmd1 = ‘cat /home/admin/deploy/log/2009-11-11.log’ cmd2 = ‘grep example.com’ cmd3 = ‘wc -l’ pipe1 = subprocess.Popen(cmd1.split(), stdout=subprocess.PIPE) pipe2 = subprocess.Popen(cmd2.split(), stdin=pipe1, stdout=subprocess.PIPE) pipe3 = subprocess.Popen(cmd3.split(), stdin=pipe2, stdout=subprocess.PIPE) result = pipe3.communicate()[0]
Published at: 12:11 am - Thursday November 05 2009
I have created a plugin for exaile which fetches album cover from douban.com You can download the plugin archive from: http://bitbucket.org/sunng/exailedoubancovers/downloads/ To install it, open exaile, click menu edit >> preference >> plugins, hit button “install plugin file”, select “doubancovers.tgz”(if you cannot find the file, just change filter to “all files”) In some cases, the [...]
Published at: 05:11 pm - Monday November 02 2009
web.py是个很小的python框架,特点就是小,连session都没有实现人家就发布了。前几天KungfuRails大会上他们吹牛说Sinatra可以写出世界上最小的Webapp,但是有web.py的化,那个最小至少要加上个“之一”。 Web.py在Lighttpd上通过fastcgi运行的配置,可以在web.py的网站上找到文档: http://webpy.org/cookbook/fastcgi-lighttpd 实际我用的时候把静态的index.html用作首页,稍改动一下: server.document-root = “/home/sun/projects/sdostatweb” server.modules += ( “mod_fastcgi” ) server.modules += ( “mod_rewrite” ) server.port = 4000 mimetype.assign = ( “.html” => “text/html” ) index-file.names = ( “index.html” ) fastcgi.server = ( “/sdostatweb.py” => (( “socket” => “/tmp/fastcgi.socket”, “bin-path” => “/home/sun/projects/sdostatweb/sdostatweb.py”, “max-procs” => 5, “bin-environment” => ( “REAL_SCRIPT_NAME” => “” ), “check-local” [...]
Published at: 05:10 pm - Tuesday October 13 2009
某人已经发展到上班时间写blog了。 继续说验证码服务,找到两个比较典型的。recaptcha非常著名,是twitter和facebook使用的验证码服务(不知道现在的情况。。。),vidoop提供了一种很有特点的验证码机制。 两个服务都提供了python的接口封装便于接入,通过api接口可以管中窥豹,大致了解这两个验证服务的机制。 recaptcha 下面是一段结合了web.py的简单调用 public_key = “******” private_key = “********” class Recaptcha(object): def GET(self): r = recaptcha.displayhtml(public_key) return render.recapt(r) def POST(self): params = web.input() recaptcha_challenge_field, recaptcha_response_field = params.recaptcha_challenge_field, params.recaptcha_response_field remote_ip = web.ctx.ip result = recaptcha.submit(recaptcha_challenge_field, recaptcha_response_field, private_key, remote_ip) return result.is_valid 我在GET请求中获取验证码,在POST请求中提交验证码。 recaptcha返回的是一段recaptcha自己风格的html片段,效果大家参考twitter的验证码,实际上是一个iframe,iframe的url中包含了哈希串。其中的字段名也自然被写死成recaptcha_challenge_field和recaptcha_response_field,考虑到应用服务器无需验证这两个field的输入,所以也无可厚非。recaptcha_response_field用于输入字符,recaptcha_challenge_field在载入时被修改为一个唯一key。 提交验证时,recaptcha需要提供以上两个用户输入和应用的privatekey以及浏览器ip。recaptcha通过recaptcha_challenge_field 应用的private key以及用户出口ip可以唯一标示用户,并包含一些冗余实现安全相关策略。 Vidoop Vidoop提供的服务机制与recaptcha大同小异 customer_id = “***” site_id = “localtest” api_username [...]
Published at: 08:09 pm - Wednesday September 30 2009
一不小心又是大概有一年多时间没有用过Django了。最近经常有之前部门的同事来讨论Django和Python Web开发,大概是他们用asp做的客服系统终于要换架构了。今天国庆提前放假,用了一个下午的时间重温一下。Django的Template还是相对弱一些,居然无法在template里调用model的方法。这样auth模块里判断用户登录的is_authticated()方法就不能在template里调,如此非得在view判断(Django是MTV模型,它的view即MVC里的controller),那如果是用generic_view,这个似乎就无解了?? 贴个图。体验一下Firefox3.5开始支持的@font-face,Webkit核心和Opera浏览器都支持这个功能。不过我Ubuntu上的Midori貌似遇到Mootools就直接崩溃,看来要等到gnome2.28里epiphany了。 忘了提了,域名是我yy的,之所以有这样的效果是修改了hosts
Published at: 09:09 pm - Tuesday September 22 2009
import MySQLdb import subprocess from smtplib import * from datetime import date, timedelta class StatEntry(object): def __init__(self, name, count): self.name = name self.count = count def __str__(self): return “%s\t%d” % (self.name, self.count) def fetchdb(date): conn = MySQLdb.connect(host=”localhost”, user=”root”, passwd=”acd”, db=”cls”) cursor = conn.cursor() cursor.execute(“SELECT link, count(*) FROM sdocom WHERE DATE(logtime) = DATE(‘%s’) GROUP BY link” [...]
Published at: 11:09 pm - Sunday September 13 2009
A simple query to test rdf and sparql. [codesyntax lang="python"] import sys import rdflib from rdflib.Graph import ConjunctiveGraph g = ConjunctiveGraph() g.parse(sys.argv[1], format=”xml”) fbns = rdflib.Namespace(“http://rdf.freebase.com/ns/”) player_refs = g.query(“”"SELECT ?player WHERE { ?root fb:soccer.football_roster_position.player ?player . }”"”, initNs={‘fb’:fbns}) players = [] for player in player_refs: gp = ConjunctiveGraph() gp.parse(player[0], format=”xml”) results = gp.query(“”"SELECT ?player_name ?player_position [...]
Published at: 09:07 pm - Thursday July 02 2009
All scripts created by paster(paster create, paster controller and etc.) uses 4 spaces as indent while many editors(vim) uses tabs automatically. Different indent in one file will cause fatal error. h.url_for is a popular helper function in many books and tutorials which to solve url mapping. This tool is no longer available by default since [...]