Category Archives: Python

dreamhost上运行python(web.py)

费了2个晚上的时间,终于搞定了,看看效果 http://dh-py.yanghao.org/ 分别用cgi和fastcgi测试了一下,看结果: cgi: [plain]/usr/sbin/ab -c 4 -n 300 http://dh-py.yanghao.org/ This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking dh-py.yanghao.org (be patient) Completed 100 requests … Continue reading

Posted in Python | 2 Comments

urllib2.urlopen超时的问题

urlopen方法在Python 2.6之前是没有timeout参数的,只能通过全局的socket超时设置,2.6之后增加了timeoute参数。 我在Mac下使用自带的Python 2.6的urlopen,没有设置timeout参数,结果在网络环境不好的情况下,时常出现read()方法没有任何反应的问题,程序卡死在read()方法里,搞了大半天,才找到问题,给urlopen加上timeout就ok了,设置了timeout之后超时之后read超时的时候会抛出socket.timeout异常 不知道其它版本有没有这个问题,还只是2.6的问题 想要程序稳定还需要给urlopen加上异常处理,再加上出现异常重试,程序就完美了。 代码: [python] fails = 0 while True: try: if fails >= 3: break f = urllib2.urlopen(url,timeout=20) page = f.read().decode(‘gbk’) except: fails += 1 else: break [/python]

Posted in Python | Tagged , , | Leave a comment

Mac下安装MySQLdb

本来以为使用easy_install安装MySQLdb很简单,结果还是费了一点劲才安装好。 问题及解决办法: easy_install MySQLdb提示 Couldn’t find index page for ‘MySQLdb’ (maybe misspelled?) No local packages or download links found for MySQLdb error: Could not find suitable distribution for Requirement.parse(‘MySQLdb’) 原来MySQLdb在pypi上面叫MySQL-python 解决办法:使用easy_install MySQL-python可以找到了,第2个问题也就出现了 easy_install MySQL-python提示 EnvironmentError: mysql_config not found 这个问题是因为找不到mysql_config的原因导致的,不知道安装时候用mysql_config做了什么,解决办法有2个,网上都是说改site.cfg里面的mysql_config路径,但我使用easy_install没办法改 解决办法:在PATH里增加/usr/local/mysql/bin解决 … Continue reading

Posted in Python | Tagged , , | 2 Comments