本文介绍了Python urllib2保持活着的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Python的urllib2创建保持活跃的HTTP请求?

解决方案

使用库。这包括支持HTTP 1.1和keepalive的urllib2的HTTP处理程序:

 >>> import urllib2 
>>>来自urlgrabber.keepalive导入HTTPHandler
>>> keepalive_handler = HTTPHandler()
>>> opener = urllib2.build_opener(keepalive_handler)
>>> urllib2.install_opener(opener)
>>>
>>> fo = urllib2.urlopen('http://www.python.org')

注意:你应使用urlgrabber版本或更早版本,作为版本3.9.1中已删除模块



有一个针对Python 3的keepalive模块的。 / p>

How can I make a "keep alive" HTTP request using Python's urllib2?

解决方案

Use the urlgrabber library. This includes an HTTP handler for urllib2 that supports HTTP 1.1 and keepalive:

>>> import urllib2
>>> from urlgrabber.keepalive import HTTPHandler
>>> keepalive_handler = HTTPHandler()
>>> opener = urllib2.build_opener(keepalive_handler)
>>> urllib2.install_opener(opener)
>>>
>>> fo = urllib2.urlopen('http://www.python.org')

Note: you should use urlgrabber version 3.9.0 or earlier, as the keepalive module has been removed in version 3.9.1

There is a port of the keepalive module to Python 3.

这篇关于Python urllib2保持活着的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:00