Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python urllib2 with keep alive

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

like image 699
ibz Avatar asked Jun 24 '09 09:06

ibz


People also ask

Is urllib2 deprecated?

urllib2 is deprecated in python 3. x. use urllib instaed.

What is the difference between Urllib and urllib2?

1) urllib2 can accept a Request object to set the headers for a URL request, urllib accepts only a URL. 2) urllib provides the urlencode method which is used for the generation of GET query strings, urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib2.

How do I use urllib2 in Python?

Simple urllib2 script urlopen('http://python.org/') print "Response:", response # Get the URL. This gets the real URL. print "The URL is: ", response. geturl() # Getting the code print "This gets the code: ", response.

Does urllib2 work in Python 3?

NOTE: urllib2 is no longer available in Python 3 You can get more idea about urllib.


1 Answers

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.

like image 75
msanders Avatar answered Sep 28 '22 08:09

msanders