Category Archives: Python

Python 2.7 中的 xreadlines unicode 中文编码陷阱

Python 2.x 自从有了 from __future__ import unicode_literals 之后编码问题貌似好多了,内部统一使用 unicode 编码,输出的终端也能根据终端的编码自动 转换,对于外部的文件读写也有了 codecs ,可以轻松搞定编码问题。 但是,但是, xreadlines 是个陷阱 通过 readlines 和 readline 读取的内容都能自动转换成 unicode 类型,而 xreadlines 还是 str 类型。

Posted in Python | Tagged , , | 1 Comment

web.py中获取当前url路径

web.ctx.homepath + web.ctx.fullpath 如果只使用web.ctx.fullpath的话,在有subapplications的应用中就不对了,在subapplications中web.ctx.fullpath是相对于当前的subapplications的,不是全部的路径 看看web.py的源代码就清楚了,下面是web.ctx的初始化代码: ctx.homedomain = ctx.protocol + ‘://’ + env.get(‘HTTP_HOST’, ‘[unknown]‘) ctx.homepath = os.environ.get(‘REAL_SCRIPT_NAME’, env.get(‘SCRIPT_NAME’, ”)) ctx.home = ctx.homedomain + ctx.homepath ctx.realhome = ctx.home ctx.ip = env.get(‘REMOTE_ADDR’) ctx.method = env.get(‘REQUEST_METHOD’) ctx.path = env.get(‘PATH_INFO’) # http://trac.lighttpd.net/trac/ticket/406 requires: if env.get(‘SERVER_SOFTWARE’, … Continue reading

Posted in Python | Tagged , | Leave a comment

python requests处理cookie中的一个bug

最近使用pyhton requests库,还不错,很好用,不过在处理cookie的时候有个问题,害我折腾半天 如果一个页面写了2个同名的cookie,但是path是不一样的,python requests只会保留最后一个cookie,也就是说requests不能处理这种同名cookie但path不一样的情况,这是由于requests在保存cookie的时候是使用dict,我想使用cookiejar的话就不会有这种问题了

Posted in Python | Tagged , | Leave a comment

urllib2.urlopen处理gzip数据

某些网站不管的请求头部带不带 Accept-Encoding:gzip 他都返回gzip压缩过的内容,也就是返回的头部都带有 Content-Encoding:gzip 对于这种网站在使用urllib2.urlopen获取数据时候由于urlopen不会自动处理gzip,得到的都是乱码,让人难以看懂的内容 对付这种情况就需要我们单独处理 f = urllib2.urlopen(url) headers = f.info() rawdata = f.read() if (‘Content-Encoding’ in headers and headers['Content-Encoding']) or \ (‘content-encoding’ in headers and headers['content-encoding']): import gzip import StringIO data = StringIO.StringIO(rawdata) gz = gzip.GzipFile(fileobj=data) rawdata = … Continue reading

Posted in Python | Tagged | Leave a comment

urllib2.urlopen方法的url中文编码问题

在urllib2.urlopen的文档中并没有提及url编码的问题,但实际使用中肯定会发现当url包含中文时会有编码异常 在实验多次之后发现只需要将传入urlopen的url编码为utf-8即可,不能是unicode哦,也不需要再做unquote处理

Posted in Python | Tagged | Leave a comment

readability的python实现

readability能获取大部份网页的主要内容,这个算法很cool,学习了一下,就写了个python版的实现,之前找过python版的实现,但都是比较老的基于0.4的,现在都1.7.1了 写了之后就有人给我要这个代码,放github给大家用吧 https://github.com/kingwkb/readability demo: http://yanghao.org/tools/readability

Posted in Python | Tagged | 5 Comments

web.py 0.36发布

距离0.35还没多久就发布了0.36,此版本主要修复0.35的问题,更新内容如下: * Upgraded to CherryPy WSGIServer 3.2.0. — #66 * Various Jython compatibility fixes (tx Ben Noordhuis) * allow strips to accept lists — #69 * Improvements to setcookie (tx lovelylain) — #65 * Added __contains__ method to Session. (tx … Continue reading

Posted in Python | Tagged | Leave a comment

web.py升级到0.35

本地升级了,简单的看了一下,之前的程序还未发现在0.35下有问题的情况,另外更新日志中所说的新的表单样式,我还未发现新的Form的render和老的有什么区别。

Posted in Python | Tagged | Leave a comment

web.py 0.35发布

ChangeLog Better ThreaedDict implementation using threadlocal (tx Ben Hoyt) Make Form a new-style class — #53 Optimized SQLQuery.join and generation of multiple_insert query — #58 New: support for Amazon’s Simple Email Service Added httponly keyword to setcookie (tx Justin Davis) … Continue reading

Posted in Python | Tagged | Leave a comment

Mac OS安装Python PIL

之前看到很多在Mac下安装PIL会有问题的内容,很多人推荐使用MacPorts安装,这个东西我不使用。 先看看到底什么问题: easy_install pil之后看到 — TKINTER support available *** JPEG support not available — ZLIB (PNG/ZIP) support available *** FREETYPE2 support not available *** LITTLECMS support not available 很明显缺少JPEG, FREETYPE2, LITTLECMS,逐个安装之后JPEG和FREETYPE2都没问题了,就LITTLECMS还是不行,在stackoverflow上查到原来LITTECMS版本只有使用1.x,不能使用2.x,重新安装LITTLECMS 1.19之后问题搞定 唯一的问题也就是PIL不兼容LITTECMS 2的问题,我找了找PIL官方也没找到这方面的说明 stackoverflow: http://stackoverflow.com/questions/5345348/what-is-the-best-way-to-install-pil-on-mac-snow-leopard-with-xcode-4-installed

Posted in Mac OS X, Python | Tagged , | Leave a comment