我需要模拟:
urllib2.urlopen(url).read(100)
但是对于压缩页面,比如:
request = urllib2.Request(url)
request.add_header('Accept-encoding', 'gzip')
response = urllib2.urlopen(request)
buf = StringIO(response.read(100))
gzip.GzipFile(fileobj=buf, mode='r').read()
IOError: CRC check failed 0xd71b7369L != 0x0L
请您参考如下方法:
尝试使用 zlib图书馆代替。 Gzip 依赖于 zlib,但引入了文件级压缩概念以及 CRC 检查,这似乎不是您想要的。
看到这个不错HTTP Compression in python文章(虽然文章提到不要直接使用 zlib,但您应该同时尝试这两种方法,并根据您具体尝试做的事情以及最适合您的方法来决定使用哪个)以及这些code snippets from Dough Hellman which show how to compress or decompress with zlib .
一些不错的阅读 Material :
- RFC 1952 - 有关 GZIP 格式的详细信息
-
zlibDocumentation
