[CPyUG:41451] 关于urllib2中的Keep-Alive及请教好用的http客户端包

View: New views
8 Messages — Rating Filter:   Alert me  

[CPyUG:41451] 关于urllib2中的Keep-Alive及请教好用的http客户端包

by hechu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

各位好:

我用python写一个http协议的网站数据搜集程序,用到urllib2库,这个程序要用使用代理服务器,并且要用登录会话,所以用到了 ProxyHandler和CookieLib中的CookieJar。用了很长时间都没问题,感觉很方便。

昨天在升级增加功能的时候,发现有个地址可能需要Connection=Keep-Alive的连接,才能正常工作,否则会出现错误。我用Firefox 发送正常请求,并抓包,与我程序发送的请求做对比,区别就在Header的Keep-Alive=300,和Connection=Keep-Alive 这两句上。我尝试在urllib2的opener中增加header来实现,发现无论如何发出的请求都是connection=close。

搜索互联网,发现已经有人提出过这个问题,并指出urllib2硬编码,导致connection保持为close。

网上有人推荐用httplib2,我粗略看了一下文档,支持cookie和proxy,但是我还非常留恋urllib2中那些其它的handler,,,

我的问题如下:
1. urllib2的keep-alive问题有没有好的解决方法?
2. 有没有好用的http客户端?httplib2貌似是个方案;twisted的web/web2不知道如何;昨晚看了一下pycurl,好像也行,,,想 听听大家的意见。

谢谢!

--~--~---------~--~----~------------~-------~--~----~
'''邮件来自Groups "python-cn"--China Py User Group
详情: http://groups-beta.google.com/group/python-cn
发言: python-cn@...
退订: python-cn-unsubscribe@...
维基: http://wiki.woodpecker.org.cn/moin/CPUG
珠江事务: http://groups.google.com/group/zpug
东南事务: http://groups.google.com/group/cpug-eastchina
北京事务: http://groups.google.com/group/bpug
中国事务: http://groups.google.com/group/CPUG
同质列表: http://python.cn/mailman/listinfo/python-chinese
'''
-~----------~----~----~----~------~----~------~--~---


[CPyUG:41454] Re: 关于urllib2中的Keep-Alive及请教好用的http客户端包

by Yongchao Lao :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hechu wrote:

> 各位好:
>
> 我用python写一个http协议的网站数据搜集程序,用到urllib2库,这个程序要
> 用使用代理服务器,并且要用登录会话,所以用到了 ProxyHandler和CookieLib
> 中的CookieJar。用了很长时间都没问题,感觉很方便。
>
> 昨天在升级增加功能的时候,发现有个地址可能需要Connection=Keep-Alive的
> 连接,才能正常工作,否则会出现错误。我用Firefox 发送正常请求,并抓包,
> 与我程序发送的请求做对比,区别就在Header的Keep-Alive=300,和Connection
> =Keep-Alive 这两句上。我尝试在urllib2的opener中增加header来实现,发现
> 无论如何发出的请求都是connection=close。
>
> 搜索互联网,发现已经有人提出过这个问题,并指出urllib2硬编码,导致
> connection保持为close。
>
> 网上有人推荐用httplib2,我粗略看了一下文档,支持cookie和proxy,但是我
> 还非常留恋urllib2中那些其它的handler,,,
>
> 我的问题如下:
> 1. urllib2的keep-alive问题有没有好的解决方法?
> 2. 有没有好用的http客户端?httplib2貌似是个方案;twisted的web/web2不知
> 道如何;昨晚看了一下pycurl,好像也行,,,想听听大家的意见。
>
> 谢谢!
>
> >
urllib2中是什么地方的硬编码导致和keep-alive不兼容?

--~--~---------~--~----~------------~-------~--~----~
'''邮件来自Groups "python-cn"--China Py User Group
详情: http://groups-beta.google.com/group/python-cn
发言: python-cn@...
退订: python-cn-unsubscribe@...
维基: http://wiki.woodpecker.org.cn/moin/CPUG
珠江事务: http://groups.google.com/group/zpug
东南事务: http://groups.google.com/group/cpug-eastchina
北京事务: http://groups.google.com/group/bpug
中国事务: http://groups.google.com/group/CPUG
同质列表: http://python.cn/mailman/listinfo/python-chinese
'''
-~----------~----~----~----~------~----~------~--~---


[CPyUG:41455] Re: 关于urllib2中的Keep-Alive及请教好用的http客户端包

by hechu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

这个问题去年有人讨论过,我也是搜索去年的邮件搜出来的相关信息。

我安装的是python2.5,在 urllib2.py 文件的第 1068 行。
也就是 AbstractHTTPHandler 这个类的 do_open 方法中。

注释说,addinfourl 类对持久链接没准备好。。。

代码片段如下:

# We want to make an HTTP/1.1 request, but the addinfourl
# class isn't prepared to deal with a persistent connection.
# It will try to read all remaining data from the socket,
# which will block while the server waits for the next request.
# So make sure the connection gets closed after the (only)
# request.
headers["Connection"] = "close"
headers = dict(
    (name.title(), val) for name, val in headers.items())
try:
    h.request(req.get_method(), req.get_selector(), req.data, headers)
    r = h.getresponse()
except socket.error, err: # XXX what error?
    raise URLError(err)



fluke.l 写道:
urllib2 中是什么地方的硬编码导致和keep-alive不兼容?

  

--~--~---------~--~----~------------~-------~--~----~
'''邮件来自Groups "python-cn"--China Py User Group
详情: http://groups-beta.google.com/group/python-cn
发言: python-cn@...
退订: python-cn-unsubscribe@...
维基: http://wiki.woodpecker.org.cn/moin/CPUG
珠江事务: http://groups.google.com/group/zpug
东南事务: http://groups.google.com/group/cpug-eastchina
北京事务: http://groups.google.com/group/bpug
中国事务: http://groups.google.com/group/CPUG
同质列表: http://python.cn/mailman/listinfo/python-chinese
'''
-~----------~----~----~----~------~----~------~--~---


[CPyUG:41471] Re: 关于urllib2中的Keep-Alive及请教好用的http客户端包

by junyi sun :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

如果你是windows平台,通过python调COM好了,连Cookie都不用自己管理,行为和IE一模一样。
而且Connection: Keep-Alive

代码大概像这样的:

http = win32com.client.Dispatch("msxml2.xmlhttp")
 http.Open("POST",url,False)
 http.send(q)

2008/2/27 hechu <hcpython@...>:
这个问题去年有人讨论过,我也是搜索去年的邮件搜出来的相关信息。

我安装的是python2.5,在 urllib2.py 文件的第 1068 行。
也就是 AbstractHTTPHandler 这个类的 do_open 方法中。

注释说,addinfourl 类对持久链接没准备好。。。

代码片段如下:

# We want to make an HTTP/1.1 request, but the addinfourl
# class isn't prepared to deal with a persistent connection.
# It will try to read all remaining data from the socket,
# which will block while the server waits for the next request.
# So make sure the connection gets closed after the (only)
# request.
headers["Connection"] = "close"
headers = dict(
    (name.title(), val) for name, val in headers.items())
try:
    h.request(req.get_method(), req.get_selector(), req.data, headers)
    r = h.getresponse()
except socket.error, err: # XXX what error?
    raise URLError(err)



fluke.l 写道:
urllib2 中是什么地方的硬编码导致和keep-alive不兼容?
  




--~--~---------~--~----~------------~-------~--~----~
'''邮件来自Groups "python-cn"--China Py User Group
详情: http://groups-beta.google.com/group/python-cn
发言: python-cn@...
退订: python-cn-unsubscribe@...
维基: http://wiki.woodpecker.org.cn/moin/CPUG
珠江事务: http://groups.google.com/group/zpug
东南事务: http://groups.google.com/group/cpug-eastchina
北京事务: http://groups.google.com/group/bpug
中国事务: http://groups.google.com/group/CPUG
同质列表: http://python.cn/mailman/listinfo/python-chinese
'''
-~----------~----~----~----~------~----~------~--~---


[CPyUG:41475] Re: 关于urllib2中的Keep-Alive及请教好用的http客户端包

by hechu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

谢谢指导。请问一下,MSXML的手册在什么地方有,我要仔细看看说明。
我的系统是Windows XP SP2,打齐了补丁,所以MSXML组件有安装升级,但是在本机搜索了一下,没有相关的帮助。
谢谢!

junyi sun 写道:
如果你是windows平台,通过python调COM好了,连Cookie都不用自己管理,行为和IE一模一样。
而且Connection: Keep-Alive

代码大概像这样的:

http = win32com.client.Dispatch("msxml2.xmlhttp")
 http.Open("POST",url,False)
 http.send(q)

2008/2/27 hechu <hcpython@...>:
这个问题去年有人讨论过,我也 是搜索去年的邮件搜出来的相关信息。

我安装的是python2.5,在 urllib2.py 文件的第 1068 行。
也就是 AbstractHTTPHandler 这个类的 do_open 方法中。

注释说,addinfourl 类对持久链接没准备好。。。

代码片段如下:

# We want to make an HTTP/1.1 request, but the addinfourl
# class isn't prepared to deal with a persistent connection.
# It will try to read all remaining data from the socket,
# which will block while the server waits for the next request.
# So make sure the connection gets closed after the (only)
# request.
headers["Connection"] = "close"
headers = dict(
    (name.title(), val) for name, val in headers.items())
try:
    h.request(req.get_method(), req.get_selector(), req.data, headers)
    r = h.getresponse()
except socket.error, err: # XXX what error?
    raise URLError(err)



fluke.l 写道:
urllib2 中是什么地方的硬编码导致和keep-alive不兼容?
  






--~--~---------~--~----~------------~-------~--~----~
'''邮件来自Groups "python-cn"--China Py User Group
详情: http://groups-beta.google.com/group/python-cn
发言: python-cn@...
退订: python-cn-unsubscribe@...
维基: http://wiki.woodpecker.org.cn/moin/CPUG
珠江事务: http://groups.google.com/group/zpug
东南事务: http://groups.google.com/group/cpug-eastchina
北京事务: http://groups.google.com/group/bpug
中国事务: http://groups.google.com/group/CPUG
同质列表: http://python.cn/mailman/listinfo/python-chinese
'''
-~----------~----~----~----~------~----~------~--~---


[CPyUG:41481] Re: 关于urllib2中的Keep-Alive及请教好用的http客户端包

by junyi sun :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

XmlHttp对象参考:
属性:
onreadystatechange* 指定当readyState属性改变时的事件处理句柄。只写
readyState 返回当前请求的状态,只读.
responseBody 将回应信息正文以unsigned byte数组形式返回.只读
responseStream 以Ado Stream对象的形式返回响应信息。只读
responseText 将响应信息作为字符串返回.只读
responseXML 将响应信息格式化为Xml Document对象并返回,只读
status 返回当前请求的http状态码.只读
statusText 返回当前请求的响应行状态,只读

* 表示此属性是W3C文档对象模型的扩展.

方法:
abort 取消当前请求
getAllResponseHeaders 获取响应的所有http头
getResponseHeader 从响应信息中获取指定的http头
open 创建一个新的http请求,并指定此请求的方法、URL以及验证信息(用户名/密码)
send 发送请求到http服务器并接收回应
setRequestHeader 单独指定请求的某个http头

statusText 返回当前请求的响应行状态,只读

* 表示此属性是W3C文档对象模型的扩展.

方法:
abort 取消当前请求


2008/2/27 hechu <hcpython@...>:
谢谢指导。请问一下,MSXML的手册在什么地方有,我要仔细看看说明。
我的系统是Windows XP SP2,打齐了补丁,所以MSXML组件有安装升级,但是在本机搜索了一下,没有相关的帮助。
谢谢!

junyi sun 写道:
如果你是windows平台,通过python调COM好了,连Cookie都不用自己管理,行为和IE一模一样。
而且Connection: Keep-Alive

代码大概像这样的:

http = win32com.client.Dispatch("msxml2.xmlhttp")
 http.Open("POST",url,False)
 http.send(q)

2008/2/27 hechu <hcpython@...>:
这个问题去年有人讨论过,我也 是搜索去年的邮件搜出来的相关信息。

我安装的是python2.5,在 urllib2.py 文件的第 1068 行。
也就是 AbstractHTTPHandler 这个类的 do_open 方法中。

注释说,addinfourl 类对持久链接没准备好。。。

代码片段如下:

# We want to make an HTTP/1.1 request, but the addinfourl
# class isn't prepared to deal with a persistent connection.
# It will try to read all remaining data from the socket,
# which will block while the server waits for the next request.
# So make sure the connection gets closed after the (only)
# request.
headers["Connection"] = "close"
headers = dict(
    (name.title(), val) for name, val in headers.items())
try:
    h.request(req.get_method(), req.get_selector(), req.data, headers)
    r = h.getresponse()
except socket.error, err: # XXX what error?
    raise URLError(err)



fluke.l 写道:
urllib2 中是什么地方的硬编码导致和keep-alive不兼容?
  









--~--~---------~--~----~------------~-------~--~----~
'''邮件来自Groups "python-cn"--China Py User Group
详情: http://groups-beta.google.com/group/python-cn
发言: python-cn@...
退订: python-cn-unsubscribe@...
维基: http://wiki.woodpecker.org.cn/moin/CPUG
珠江事务: http://groups.google.com/group/zpug
东南事务: http://groups.google.com/group/cpug-eastchina
北京事务: http://groups.google.com/group/bpug
中国事务: http://groups.google.com/group/CPUG
同质列表: http://python.cn/mailman/listinfo/python-chinese
'''
-~----------~----~----~----~------~----~------~--~---


[CPyUG:41491] Re: 关于urllib2中的Keep-Alive及请教好用的http客户端包

by hechu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

呵呵,谢谢!
我在网上找到一个比较全的文档,
http://news.lnwest.com/program/xmlhttp/
这会儿正在改程序。

junyi sun 写道:
XmlHttp对象参考:
属性:
onreadystatechange* 指定当readyState属性改变时的事件处理句柄。只写
readyState 返回当前请求的状态,只读.
responseBody 将回应信息正文以unsigned byte数组形式返回.只读
responseStream 以Ado Stream对象的形式返回响应信息。只读
responseText 将响应信息作为字符串返回.只读
responseXML 将响应信息格式化为Xml Document对象并返回,只读
status 返回当前请求的http状态码.只读
statusText 返回当前请求的响应行状态,只读

* 表示此属性是W3C文档对象模型的扩展.

方法:
abort 取消当前请求
getAllResponseHeaders 获取响应的所有http头
getResponseHeader 从响应信息中获取指定的http头
open 创建一个新的http请求,并指定此请求的方法、URL以及验证信息(用户名/密码)
send 发送请求到http服务器并接收回应
setRequestHeader 单独指定请求的某个http头

statusText 返回当前请求的响应行状态,只读

* 表示此属性是W3C文档对象模型的扩展.

方法:
abort 取消当前请求


2008/2/27 hechu <hcpython@...>:
谢谢指导。请问一下,MSXML的手册在什么地方有,我要仔细看 看说明。
我的系统是Windows XP SP2,打齐了补丁,所以MSXML组件有安装升级,但是在本机搜索了一下,没有相关的帮助。
谢谢!

junyi sun 写道:
如果你是windows平台,通过python调COM好了,连Cookie都不用自己管理, 行为和IE一模一样。
而且Connection: Keep-Alive

代码大概像这样的:

http = win32com.client.Dispatch("msxml2.xmlhttp")
 http.Open("POST",url,False)
 http.send(q)

2008/2/27 hechu <hcpython@...>:
这个问题去年有人讨论 过,我也 是搜索去年的邮件搜出来的相关信息。

我安装的是python2.5,在 urllib2.py 文件的第 1068 行。
也就是 AbstractHTTPHandler 这个类的 do_open 方法中。

注释说,addinfourl 类对持久链接没准备好。。。

代码片段如下:

# We want to make an HTTP/1.1 request, but the addinfourl
# class isn't prepared to deal with a persistent connection.
# It will try to read all remaining data from the socket,
# which will block while the server waits for the next request.
# So make sure the connection gets closed after the (only)
# request.
headers["Connection"] = "close"
headers = dict(
    (name.title(), val) for name, val in headers.items())
try:
    h.request(req.get_method(), req.get_selector(), req.data, headers)
    r = h.getresponse()
except socket.error, err: # XXX what error?
    raise URLError(err)



fluke.l 写道:
urllib2 中是什么地方的硬编码导致和keep-alive不兼容?
  











--~--~---------~--~----~------------~-------~--~----~
'''邮件来自Groups "python-cn"--China Py User Group
详情: http://groups-beta.google.com/group/python-cn
发言: python-cn@...
退订: python-cn-unsubscribe@...
维基: http://wiki.woodpecker.org.cn/moin/CPUG
珠江事务: http://groups.google.com/group/zpug
东南事务: http://groups.google.com/group/cpug-eastchina
北京事务: http://groups.google.com/group/bpug
中国事务: http://groups.google.com/group/CPUG
同质列表: http://python.cn/mailman/listinfo/python-chinese
'''
-~----------~----~----~----~------~----~------~--~---


[CPyUG:41585] Re: 关于urllib2中的Keep-Alive及请教好用的http客户端包

by jigloo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

pycurl又简单又强大又快速。

On 2月27日, 下午2时39分, hechu <hcpyt...@...> wrote:
> 各位好:
> 我用python写一个http协议的网站数据搜集程序,用到urllib2库,这个程序要用使用代理服务器,并且要用登录会话,所以用到了 ProxyHandler和CookieLib中的CookieJar。用了很长时间都没问题,感觉很方便。
> 昨天在升级增加功能的时候,发现有个地址可能需要Connection=Keep-Alive的连接,才能正常工作,否则会出现错误。我用Firefox 发送正常请求,并抓包,与我程序发送的请求做对比,区别就在Header的Keep-Alive=300,和Connection=Keep-Alive 这两句上。我尝试在urllib2的opener中增加header来实现,发现无论如何发出的请求都是connection=close。
> 搜索互联网,发现已经有人提出过这个问题,并指出urllib2硬编码,导致connection保持为close。
> 网上有人推荐用httplib2,我粗略看了一下文档,支持cookie和proxy,但是我还非常留恋urllib2中那些其它的handler,,,
> 我的问题如下:
> 1. urllib2的keep-alive问题有没有好的解决方法?
> 2. 有没有好用的http客户端?httplib2貌似是个方案;twisted的web/web2不知道如何;昨晚看了一下pycurl,好像也行,,,想 听听大家的意见。
> 谢谢!
--~--~---------~--~----~------------~-------~--~----~
'''邮件来自Groups "python-cn"--China Py User Group
详情: http://groups-beta.google.com/group/python-cn
发言: python-cn@...
退订: python-cn-unsubscribe@...
维基: http://wiki.woodpecker.org.cn/moin/CPUG
珠江事务: http://groups.google.com/group/zpug
东南事务: http://groups.google.com/group/cpug-eastchina
北京事务: http://groups.google.com/group/bpug
中国事务: http://groups.google.com/group/CPUG
同质列表: http://python.cn/mailman/listinfo/python-chinese
'''
-~----------~----~----~----~------~----~------~--~---