Search code examples
pythonhttpurllib2keep-alive

Python urllib2 with keep alive


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


Solution

  • 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.