Tryag File Manager
Home
-
Turbo Force
Current Path :
/
usr
/
lib
/
python2.4
/
site-packages
/
urlgrabber
/
Upload File :
New :
File
Dir
//usr/lib/python2.4/site-packages/urlgrabber/keepalive.pyc
mò 5Ec @ s¹ d Z d k Z d k Z d k Z d k Z d a d k Z d k Z e i d j o d a n d a d f d „ ƒ YZ d f d „ ƒ YZ d e e i f d „ ƒ YZ d e e i f d „ ƒ YZ d e i f d „ ƒ YZ d e i f d „ ƒ YZ d e i f d „ ƒ YZ d „ Z d „ Z d „ Z d d „ Z d „ Z d d „ Z e d j oa d k Z d k Z y$ e e i d ƒ Z e i d Z Wn d e i d GHqµXe e e ƒ n d S( s 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. Ni i i i t ConnectionManagerc B sG t Z d Z d „ Z d „ Z d „ Z d „ Z d „ Z e d „ Z RS( sV The connection manager must be able to: * keep track of all existing c C s. t i ƒ | _ h | _ h | _ h | _ d S( N( t threadt allocate_lockt selft _lockt _hostmapt _connmapt _readymap( R ( ( t8 /usr/lib/python2.4/site-packages/urlgrabber/keepalive.pyt __init__z s c C sx | i i ƒ zV | i i | ƒ p g | i | <n | i | i | ƒ | | i | <| | i | <Wd | i i ƒ Xd S( N( R R t acquireR t has_keyt hostt appendt connectionR t readyR t release( R R R R ( ( R t add€ s c C s‘ | i i ƒ zo y | i | } Wn t j o nF X| i | =| i | =| i | i | ƒ | i | p | i | =n Wd | i i ƒ Xd S( N( R R R R R R t KeyErrorR R t removeR ( R R R ( ( R R Š s c C s+ y | | i | <Wn t j o n Xd S( N( R R R R R ( R R R ( ( R t set_ready™ s c C s d } | i i ƒ zY | i i | ƒ oB x? | i | D], } | i | o d | i | <| } Pq7 q7 Wn Wd | i i ƒ X| S( Ni ( t Nonet connR R R R R R t cR R ( R R R R ( ( R t get_ready_conn s c C s5 | o t | i i | g ƒ ƒ Sn t | i ƒ Sd S( N( R t listR R t gett dict( R R ( ( R t get_all« s ( t __name__t __module__t __doc__R R R R R R R ( ( ( R R u s t KeepAliveHandlerc B se t Z d „ Z d „ Z d „ Z d „ Z d „ Z d d „ Z d „ Z d „ Z d „ Z d „ Z RS( Nc C s t ƒ | _ d S( N( R R t _cm( R ( ( R R ² s c C s@ g } | i i ƒ i ƒ D] \ } } | | t | ƒ f q ~ S( st return a list of connected hosts and the number of connections to each. [('foo.com:80', 2), ('bar.org', 1)]N( t _[1]R R! R t itemsR t lit len( R R"