URN Logo
UNIX Resources » Linux » China Linux Forum » Python 编 程 » 4 » python httplib.respose.read()怎样接收gzip数据
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
python httplib.respose.read()怎样接收gzip数据
 
 
 
Subject: python httplib.respose.read()怎样接收gzip数据
Author: junchangyang    Posted: 2005-12-04 15:17    Length: 409 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]
Subject: Re: python httplib.respose.read()怎样接收gzip数据
Author: junchangyang    Posted: 2005-12-04 21:27    Length: 4,610 byte(s)
[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]
Subject: Re: python httplib.respose.read()怎样接收gzip数据
Author: passworld    Posted: 2005-12-05 10:54    Length: 534 byte(s)
[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行求平均
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 04:10:57, cost 0.040301084518433 ms.