|
|
|
|
| python httplib.respose.read()怎样接收gzip数据 |
 python httplib.respose.read()怎样接收gzip数据 - junchangyang [ 2005-12-04 15:17 | 409 byte(s)]
 Re: python httplib.respose.read()怎样接收gzip数据 - junchangyang [ 2005-12-04 21:27 | 4,610 byte(s)]
 Re: python httplib.respose.read()怎样接收gzip数据 - passworld [ 2005-12-05 10:54 | 534 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
我用httplib.response.read()读不到gzip数据,我要怎样才能读呢,
在urllib下的_fileobject类中,定义的read()好象(它注解说的)只能读取string
,我用什么方法读响应的gzip格式的内容呢。
用socket.read()可以接收,但用httlib.response.read()却收不到,是什么回事呢,我怎样从
fh = urllib2.urlopen(fhRequest) 返回的file_like对象中提取出socket以收发二进制数据呢?
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
我在网上找的一点资料,但因我是刚开始学,没有看明白,请大侠们指点一下!
http://groups.google.com/group/comp.lang.python/browse_frm/thread/c30b089f13746db4/2800b502046c1f07?q=response.read&rnum=3#2800b502046c1f07
4. John J. Lee
2003年7月30日 上午5时39分 显示选项
新闻论坛:comp.lang.python
发件人: j...@pobox.com (John J. Lee) - 查找此作者的帖子
日期:29 Jul 2003 22:39:36 +0100
当地时间:2003年7月30日(星期三) 上午5时39分
主题:Re: gzip HTTP results problem
答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为
- 隐藏被引用文字 -
- 显示引用的文字 -
"Fredrik Lundh" <fred...@pythonware.com> writes:
> Bill Loren wrote:
> > I've encountered a problem trying to decode gzip data
> > returned from an HTTP server I communicate with (I use urllib2).
> > I've tried to use both the gzip and zlib come-along python libraries but
> > alas.
> > Have anyone of you ppl succeeded in talking gzip with an HTTP
> > server ?
> this might help:
> http://effbot.org/zone/consumer-gzip.htm
> (that piece of code is used in production code, so it should
> work...)
That would go nicely with my unreleased latest version of
ClientCookie, which is plug-compatible with urllib2. but makes it
easier to add new functionality like this (I submitted a patch to
Python library based on this a while back, and am hoping for
comments).
The idea is that you pass processor objects to build_opener just as if
they were handler objects. Processors pre-process requests and
post-process responses. This stops you having to subclass things like
AbstractHTTPHandler. Ask if you want a copy.
(code below is completely untested, unworking, purely illustrative!)
import ClientCookie
from GzipConsumer import GzipConsumer
from cStringIO import StringIO
class stupid_gzip_consumer:
def __init__(self): self.data = []
def feed(self, data): self.data.append(data)
class stupid_gzip_wrapper:
def __init__(self, response):
self._response = response
c = stupid_gzip_consumer()
gzc = GzipConsumer(c)
gzc.feed(response.read())
self.__data = StringIO("".join(c.data))
def __getattr__(self, key):
# delegate unknown methods/attributes
if key in ("read", "readline", "readlines"):
return getattr(self.__data, key)
else:
return getattr(self._response, key)
class HTTPGzipProcessor(ClientCookie.BaseProcessor):
def http_response(self, request, response):
# post-process response
enc = response.hdrs.get["content-encoding"]
if ("gzip" in enc) or ("compress" in enc):
return stupid_gzip_wrapper(response)
else:
return response
https_response = http_response
opener = ClientCookie.build_opener(HTTPGzipProcessor)
ClientCookie.install_opener(opener)
response = urlopen("http://www.example.com/")
print response.read()
response.close()
John
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
基本上就是你httplib 不支持gzip,所以直接用它无法得到一个 respose object.你可以用urllib2,自己subclass BaseHandler 定义一个 GzipHandler,定义一个 http_response()方法来解压缩gzip的内容,然后把解开的内容返回,让下一个handler来继续处理。
解压缩的具体步骤你可以参考你找到的例子和 urllib2.py 里的源码。
如果你非要用 httplib,你就要自己 subclass HTTPResponse ,让它先解码再处理,然后在 HTTPConection里设定response_class 为你的新HTTPResponse。参看 httplib 的源码。
|
|
|
----
|
|
[Original]
[Print]
[Top]
|
|
« Previous thread
各位帮忙看看我的函数应该怎么改 |
Python 编 程
第4页 |
Next thread »
每60行求平均 |
|