urlgrabber.keepalive
index
/home/groups/urlgrabber/web/contents/urlgrabber/keepalive.py

An HTTP handler for urllib2 that supports HTTP 1.1 and keepalive.
 
>>> import urllib2
>>> from keepalive import HTTPHandler
>>> keepalive_handler = HTTPHandler()
>>> opener = urllib2.build_opener(keepalive_handler)
>>> urllib2.install_opener(opener)
>>> 
>>> fo = urllib2.urlopen('http://www.python.org')
 
If a connection to a given host is requested, and all of the existing
connections are still in use, another connection will be opened.  If
the handler tries to use an existing connection but it fails in some
way, it will be closed and removed from the pool.
 
To remove the handler, simply re-run build_opener with no arguments, and
install that opener.
 
You can explicitly close connections by using the close_connection()
method of the returned file-like object (described below) or you can
use the handler methods:
 
  close_connection(host)
  close_all()
  open_connections()
 
NOTE: using the close_connection and close_all methods of the handler
should be done with care when using multiple threads.
  * there is nothing that prevents another thread from creating new
    connections immediately after connections are closed
  * no checks are done to prevent in-use connections from being closed
 
>>> keepalive_handler.close_all()
 
EXTRA ATTRIBUTES AND METHODS
 
  Upon a status of 200, the object returned has a few additional
  attributes and methods, which should not be used if you want to
  remain consistent with the normal urllib2-returned objects:
 
    close_connection()  -  close the connection to the host
    readlines()         -  you know, readlines()
    status              -  the return status (ie 404)
    reason              -  english translation of status (ie 'File not found')
 
  If you want the best of both worlds, use this inside an
  AttributeError-catching try:
 
  >>> try: status = fo.status
  >>> except AttributeError: status = None
 
  Unfortunately, these are ONLY there if status == 200, so it's not
  easy to distinguish between non-200 responses.  The reason is that
  urllib2 tries to do clever things with error codes 301, 302, 401,
  and 407, and it wraps the object upon return.
 
  For python versions earlier than 2.4, you can avoid this fancy error
  handling by setting the module-level global HANDLE_ERRORS to zero.
  You see, prior to 2.4, it's the HTTP Handler's job to determine what
  to handle specially, and what to just pass up.  HANDLE_ERRORS == 0
  means "pass everything up".  In python 2.4, however, this job no
  longer belongs to the HTTP Handler and is now done by a NEW handler,
  HTTPErrorProcessor.  Here's the bottom line:
 
    python version < 2.4
        HANDLE_ERRORS == 1  (default) pass up 200, treat the rest as
                            errors
        HANDLE_ERRORS == 0  pass everything up, error processing is
                            left to the calling code
    python version >= 2.4
        HANDLE_ERRORS == 1  pass up 200, treat the rest as errors
        HANDLE_ERRORS == 0  (default) pass everything up, let the
                            other handlers (specifically,
                            HTTPErrorProcessor) decide what to do
 
  In practice, setting the variable either way makes little difference
  in python 2.4, so for the most consistent behavior across versions,
  you probably just want to use the defaults, which will give you
  exceptions on errors.

 
Modules
       
httplib
socket
urlgrabber.sslfactory
sys
thread
urllib2

 
Classes
       
httplib.HTTPConnection
HTTPConnection
httplib.HTTPResponse
HTTPResponse
httplib.HTTPSConnection(httplib.HTTPConnection)
HTTPSConnection
ConnectionManager
KeepAliveHandler
HTTPHandler(KeepAliveHandler, urllib2.HTTPHandler)
HTTPSHandler(KeepAliveHandler, urllib2.HTTPSHandler)

 
class ConnectionManager
    The connection manager must be able to:
  * keep track of all existing
 
  Methods defined here:
__init__(self)
add(self, host, connection, ready)
get_all(self, host=None)
get_ready_conn(self, host)
remove(self, connection)
set_ready(self, connection, ready)

 
class HTTPConnection(httplib.HTTPConnection)
     Data and other attributes defined here:
response_class = <class urlgrabber.keepalive.HTTPResponse>

Methods inherited from httplib.HTTPConnection:
__init__(self, host, port=None, strict=None)
close(self)
Close the connection to the HTTP server.
connect(self)
Connect to the host and port specified in __init__.
endheaders(self)
Indicate that the last header line has been sent to the server.
getresponse(self)
Get the response from the server.
putheader(self, header, value)
Send a request header line to the server.
 
For example: h.putheader('Accept', 'text/html')
putrequest(self, method, url, skip_host=0, skip_accept_encoding=0)
Send a request to the server.
 
`method' specifies an HTTP request method, e.g. 'GET'.
`url' specifies the object being requested, e.g. '/index.html'.
`skip_host' if True does not add automatically a 'Host:' header
`skip_accept_encoding' if True does not add automatically an
   'Accept-Encoding:' header
request(self, method, url, body=None, headers={})
Send a complete request to the server.
send(self, str)
Send `str' to the server.
set_debuglevel(self, level)

Data and other attributes inherited from httplib.HTTPConnection:
auto_open = 1
debuglevel = 0
default_port = 80
strict = 0

 
class HTTPHandler(KeepAliveHandler, urllib2.HTTPHandler)
    
Method resolution order:
HTTPHandler
KeepAliveHandler
urllib2.HTTPHandler
urllib2.AbstractHTTPHandler
urllib2.BaseHandler

Methods defined here:
__init__(self)
http_open(self, req)

Methods inherited from KeepAliveHandler:
close_all(self)
close all open connections
close_connection(self, host)
close connection(s) to <host>
host is the host:port spec, as in 'www.cnn.com:8080' as passed in.
no error occurs if there is no connection to that host.
do_open(self, req)
#### Transaction Execution
open_connections(self)
return a list of connected hosts and the number of connections
to each.  [('foo.com:80', 2), ('bar.org', 1)]

Methods inherited from urllib2.HTTPHandler:
http_request = do_request_(self, request)

Methods inherited from urllib2.AbstractHTTPHandler:
do_request_(self, request)
set_http_debuglevel(self, level)

Methods inherited from urllib2.BaseHandler:
__lt__(self, other)
add_parent(self, parent)
close(self)

Data and other attributes inherited from urllib2.BaseHandler:
handler_order = 500

 
class HTTPResponse(httplib.HTTPResponse)
     Methods defined here:
__init__(self, sock, debuglevel=0, strict=0, method=None)
close(self)
close_connection(self)
geturl(self)
info(self)
read(self, amt=None)
readline(self, limit=-1)
readlines(self, sizehint=0)

Methods inherited from httplib.HTTPResponse:
begin(self)
getheader(self, name, default=None)
getheaders(self)
Return list of (header, value) tuples.
isclosed(self)

 
class HTTPSConnection(httplib.HTTPSConnection)
    
Method resolution order:
HTTPSConnection
httplib.HTTPSConnection
httplib.HTTPConnection

Data and other attributes defined here:
response_class = <class urlgrabber.keepalive.HTTPResponse>

Methods inherited from httplib.HTTPSConnection:
__init__(self, host, port=None, key_file=None, cert_file=None, strict=None)
connect(self)
Connect to a host on a given (SSL) port.

Data and other attributes inherited from httplib.HTTPSConnection:
default_port = 443

Methods inherited from httplib.HTTPConnection:
close(self)
Close the connection to the HTTP server.
endheaders(self)
Indicate that the last header line has been sent to the server.
getresponse(self)
Get the response from the server.
putheader(self, header, value)
Send a request header line to the server.
 
For example: h.putheader('Accept', 'text/html')
putrequest(self, method, url, skip_host=0, skip_accept_encoding=0)
Send a request to the server.
 
`method' specifies an HTTP request method, e.g. 'GET'.
`url' specifies the object being requested, e.g. '/index.html'.
`skip_host' if True does not add automatically a 'Host:' header
`skip_accept_encoding' if True does not add automatically an
   'Accept-Encoding:' header
request(self, method, url, body=None, headers={})
Send a complete request to the server.
send(self, str)
Send `str' to the server.
set_debuglevel(self, level)

Data and other attributes inherited from httplib.HTTPConnection:
auto_open = 1
debuglevel = 0
strict = 0

 
class HTTPSHandler(KeepAliveHandler, urllib2.HTTPSHandler)
    
Method resolution order:
HTTPSHandler
KeepAliveHandler
urllib2.HTTPSHandler
urllib2.AbstractHTTPHandler
urllib2.BaseHandler

Methods defined here:
__init__(self, ssl_factory=None)
https_open(self, req)

Methods inherited from KeepAliveHandler:
close_all(self)
close all open connections
close_connection(self, host)
close connection(s) to <host>
host is the host:port spec, as in 'www.cnn.com:8080' as passed in.
no error occurs if there is no connection to that host.
do_open(self, req)
#### Transaction Execution
open_connections(self)
return a list of connected hosts and the number of connections
to each.  [('foo.com:80', 2), ('bar.org', 1)]

Methods inherited from urllib2.HTTPSHandler:
https_request = do_request_(self, request)

Methods inherited from urllib2.AbstractHTTPHandler:
do_request_(self, request)
set_http_debuglevel(self, level)

Methods inherited from urllib2.BaseHandler:
__lt__(self, other)
add_parent(self, parent)
close(self)

Data and other attributes inherited from urllib2.BaseHandler:
handler_order = 500

 
class KeepAliveHandler
     Methods defined here:
__init__(self)
close_all(self)
close all open connections
close_connection(self, host)
close connection(s) to <host>
host is the host:port spec, as in 'www.cnn.com:8080' as passed in.
no error occurs if there is no connection to that host.
do_open(self, req)
#### Transaction Execution
open_connections(self)
return a list of connected hosts and the number of connections
to each.  [('foo.com:80', 2), ('bar.org', 1)]

 
Functions
       
comp(N, url)
continuity(url)
error_handler(url)
fetch(N, url, delay=0)
test(url, N=10)
test_timeout(url)

 
Data
        DEBUG = None
HANDLE_ERRORS = 0