;ò
µAc           @   s;  d  Z  d k Z d k Z d k Z d d f Z d a d „  a d a 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 GHq7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')

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()

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

  You can optionally set the module-level global HANDLE_ERRORS to 0,
  in which case the handler will always return the object directly.
  If you like the fancy handling of errors, don't do this.  If you
  prefer to see your error codes, then do.

Ni    i   c          G   s   d i  |  ƒ GHd  S(   Ns    (   s   joins   args(   s   args(    (    s   /usr/share/yum/keepalive.pys   DBPRINT=   s    i   s   HTTPHandlerc           B   sS   t  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d	 „  Z	 RS(
   Nc         C   s   h  |  _ d  S(   N(   s   selfs   _connections(   s   self(    (    s   /usr/share/yum/keepalive.pys   __init__A   s    c         C   s   |  i | d d ƒd S(   s¥   close connection 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.s   closei   N(   s   selfs   _remove_connections   host(   s   selfs   host(    (    s   /usr/share/yum/keepalive.pys   close_connectionD   s     c         C   s   |  i i ƒ  Sd S(   s    return a list of connected hostsN(   s   selfs   _connectionss   keys(   s   self(    (    s   /usr/share/yum/keepalive.pys   open_connectionsJ   s     c         C   s7   x' |  i i ƒ  D] \ } } | i ƒ  q Wh  |  _ d S(   s   close all open connectionsN(   s   selfs   _connectionss   itemss   hosts   conns   close(   s   selfs   hosts   conn(    (    s   /usr/share/yum/keepalive.pys	   close_allN   s
      i    c         C   sA   |  i i | ƒ o* | o |  i | i ƒ  n |  i | =n d  S(   N(   s   selfs   _connectionss   has_keys   hosts   close(   s   selfs   hosts   close(    (    s   /usr/share/yum/keepalive.pys   _remove_connectionT   s     c         C   sZ  y§ | i ƒ  o€ | i ƒ  } | i d | i ƒ  ƒ | i i d ƒ o | i	 d d ƒ n | i i d ƒ o | i	 d d t
 | ƒ ƒ q¦ n | i d | i ƒ  ƒ Wn0 t i t i f j
 o } t i | ƒ ‚ n Xx! |  i i D] } | i	 | Œ  qç Wx- | i i ƒ  D] \ } } | i	 | | ƒ qW| i ƒ  | i ƒ  o | i | ƒ n d  S(   Ns   POSTs   Content-types!   application/x-www-form-urlencodeds   Content-lengths   %ds   GET(   s   reqs   has_datas   get_datas   datas   hs
   putrequests   get_selectors   headerss   has_keys	   putheaders   lens   sockets   errors   httplibs   HTTPExceptions   errs   urllib2s   URLErrors   selfs   parents
   addheaderss   argss   itemss   ks   vs
   endheaderss   send(   s   selfs   hs   reqs   argss   errs   vs   datas   k(    (    s   /usr/share/yum/keepalive.pys   _start_connectionY   s*    	"  
c         C   sn  | i ƒ  } | o t i d ƒ ‚ n ygd } |  i i | ƒ } | t
 j oä y  |  i | | ƒ | i ƒ  } WnZ t i t i f j
 o t
 } n7 t o t d | ƒ n |  i | d d ƒ‚  n X| t
 j p | i d j o' t o t d | ƒ n | i ƒ  q4t o t d | ƒ n d } n | oR t o t d	 | ƒ n | | ƒ } | |  i | <|  i | | ƒ | i ƒ  } n Wn0 t i t i f j
 o } t i | ƒ ‚ n X| i o |  i | ƒ n t o t d
 | i | i f ƒ n |  | _ | | _ | i ƒ  | _ | i d j p t  o | Sn) |  i! i d | | | i | i | i" ƒ Sd  S(   Ns   no host giveni   s/   unexpected exception - closing connection to %ss   closei	   s!   failed to re-use connection to %ss   re-using connection to %si    s   creating new connection to %ss   STATUS: %s, %siÈ   s   http(#   s   reqs   get_hosts   hosts   urllib2s   URLErrors   need_new_connections   selfs   _connectionss   gets   hs   Nones   _start_connections   getresponses   rs   sockets   errors   httplibs   HTTPExceptions   DEBUGs   DBPRINTs   _remove_connections   versions   closes
   http_classs   errs
   will_closes   statuss   reasons   _handlers   _hosts   get_full_urls   _urls   HANDLE_ERRORSs   parents   msg(   s   selfs
   http_classs   reqs   need_new_connections   errs   hs   hosts   r(    (    s   /usr/share/yum/keepalive.pys   do_openp   sV    

   
 
  		c         C   s   |  i t | ƒ Sd  S(   N(   s   selfs   do_opens   HTTPConnections   req(   s   selfs   req(    (    s   /usr/share/yum/keepalive.pys	   http_open¯   s    (
   s   __name__s
   __module__s   __init__s   close_connections   open_connectionss	   close_alls   _remove_connections   _start_connections   do_opens	   http_open(    (    (    s   /usr/share/yum/keepalive.pys   HTTPHandler@   s   						?s   HTTPResponsec           B   se   t  Z d d e d „ Z e i i Z d „  Z d „  Z	 d „  Z
 e d „ Z d d „ Z d d	 „ Z RS(
   Ni    c         C   sw   | o t i i |  | | | ƒ n t i i |  | | ƒ | i |  _ d |  _ d |  _	 t
 |  _ t
 |  _ t
 |  _ d  S(   Ns    i   (   s   methods   httplibs   HTTPResponses   __init__s   selfs   socks
   debuglevels   filenos   _rbufs	   _rbufsizes   Nones   _handlers   _hosts   _url(   s   selfs   socks
   debuglevels   stricts   method(    (    s   /usr/share/yum/keepalive.pys   __init__Ç   s    				c         C   s'   |  i ƒ  |  i i |  i d d ƒd  S(   Ns   closei   (   s   selfs   closes   _handlers   _remove_connections   _host(   s   self(    (    s   /usr/share/yum/keepalive.pys   close_connectionÕ   s    
c         C   s   |  i Sd  S(   N(   s   selfs   msg(   s   self(    (    s   /usr/share/yum/keepalive.pys   infoÙ   s    c         C   s   |  i Sd  S(   N(   s   selfs   _url(   s   self(    (    s   /usr/share/yum/keepalive.pys   geturlÜ   s    c         C   sŽ   |  i o | t j oO t |  i ƒ } | | j o | | 8} qg |  i |  } |  i | |  _ | Sn |  i |  i | ƒ } d |  _ | Sd  S(   Ns    (   s   selfs   _rbufs   amts   Nones   lens   Ls   ss	   _raw_read(   s   selfs   amts   Ls   s(    (    s   /usr/share/yum/keepalive.pys   readß   s    	iÿÿÿÿc         C   s=  d } |  i i d ƒ } xœ | d j  o% d | j  o t |  i ƒ j n of |  i |  i ƒ } | o Pn | i d ƒ } | d j o | t |  i ƒ } n |  i | |  _ q W| d j  o t |  i ƒ } n | d } d | j o t |  i ƒ j  n o
 | } n |  i |  |  i | f \ } |  _ | Sd  S(   Ns    s   
i    i   (
   s   datas   selfs   _rbufs   finds   is   limits   lens	   _raw_reads	   _rbufsizes   new(   s   selfs   limits   is   news   data(    (    s   /usr/share/yum/keepalive.pys   readlineï   s$     5   
' 
#c         C   sr   d } g  } x[ n oS |  i ƒ  } | o Pn | i | ƒ | t | ƒ 7} | o
 | | j o Pq q W| Sd  S(   Ni    i   (   s   totals   lists   selfs   readlines   lines   appends   lens   sizehint(   s   selfs   sizehints   lists   lines   total(    (    s   /usr/share/yum/keepalive.pys	   readlinesþ   s      
(   s   __name__s
   __module__s   Nones   __init__s   httplibs   HTTPResponses   reads	   _raw_reads   close_connections   infos   geturls   readlines	   readlines(    (    (    s   /usr/share/yum/keepalive.pys   HTTPResponse²   s   			s   HTTPConnectionc           B   s   t  Z e Z RS(   N(   s   __name__s
   __module__s   HTTPResponses   response_class(    (    (    s   /usr/share/yum/keepalive.pys   HTTPConnection  s   c         C   sE  t  } t ƒ  }
 t i |
 ƒ } t i | ƒ h  d d <d d <} xÍ d d f D]¿ } d | | | f GH| a  ym t i
 |  ƒ }	 |	 i ƒ  } |	 i ƒ  y |	 i |	 i f \ } } Wn% t j
 o t t f \ } } n XWn! t j
 o } d | GH‚  qP Xd | | f GHqP W| a  |
 i ƒ  } d Gd	 i | ƒ GH|
 i ƒ  d  S(
   Ni    s   offi   s   ons.     fancy error handling %s (HANDLE_ERRORS = %i)s     EXCEPTION: %ss     status = %s, reason = %ss   open connections:s    (   s   HANDLE_ERRORSs   origs   HTTPHandlers   keepalive_handlers   urllib2s   build_openers   openers   install_openers   poss   is   urlopens   urls   fos   reads   foos   closes   statuss   reasons   AttributeErrors   Nones   IOErrors   es   open_connectionss   hostss   joins	   close_all(   s   urls   statuss   foos   es   is   reasons   openers   poss   hostss   fos   keepalive_handlers   orig(    (    s   /usr/share/yum/keepalive.pys   error_handler  s4     	 
  	c         C   sQ  d  k  } d } t i ƒ  } t i | ƒ t i |  ƒ } | i	 ƒ  } | i ƒ  | i | ƒ } | d | i ƒ  f GHt i t ƒ  ƒ } t i | ƒ t i |  ƒ } | i	 ƒ  } | i ƒ  | i | ƒ } | d | i ƒ  f GHt i |  ƒ } d } x. n o& | i ƒ  } | o | | } qñ Pqø W| i ƒ  | i | ƒ } | d | i ƒ  f GHd  S(   Ns   %25s: %ss   normal urllibs   keepalive reads    i   s   keepalive readline(   s   md5s   formats   urllib2s   build_openers   openers   install_openers   urlopens   urls   fos   reads   foos   closes   news   ms	   hexdigests   HTTPHandlers   readlines   f(   s   urls   foos   formats   fs   ms   openers   md5s   fo(    (    s   /usr/share/yum/keepalive.pys
   continuity-  s6    	

  
c         C   s«   d |  | f GHt i i d ƒ t i ƒ  } t i | ƒ t	 |  | ƒ } d | GHt i i d ƒ t i t ƒ  ƒ } t i | ƒ t	 |  | ƒ } d | GHd | | f GHd  S(   Ns      making %i connections to:
  %ss(     first using the normal urllib handlerss     TIME: %.3f ss(     now using the keepalive handler       s     improvement factor: %.2f(   s   Ns   urls   syss   stdouts   writes   urllib2s   build_openers   openers   install_openers   fetchs   t1s   HTTPHandlers   t2(   s   Ns   urls   t2s   t1s   opener(    (    s   /usr/share/yum/keepalive.pys   compN  s    		c   
      C   sè   g  } t i ƒ  } xq t |  ƒ D]c } | o
 | d j o t i | ƒ n t i	 | ƒ }	 |	 i ƒ  } |	 i ƒ  | i t | ƒ ƒ q Wt i ƒ  | } d } xA | d D]5 } | d } | | d j o d | | f GHq§ q§ W| Sd  S(   Ni    i   s+   WARNING: inconsistent length on read %i: %i(   s   lenss   times	   starttimes   ranges   Ns   is   delays   sleeps   urllib2s   urlopens   urls   fos   reads   foos   closes   appends   lens   diffs   j(
   s   Ns   urls   delays   lenss   js   is	   starttimes   diffs   foos   fo(    (    s   /usr/share/yum/keepalive.pys   fetch`  s$      
 
c         C   sü   t  } d „  a  d a d GHt i |  ƒ } | i ƒ  } | i	 ƒ  d GHd } xJ | d j o< t i i d | ƒ t i i ƒ  t i d ƒ | d 8} qM Wt i i d ƒ d	 GHt i |  ƒ } | i ƒ  } | i	 ƒ  | | j o	 d
 GHn d GHd a | a  d  S(   Nc          G   s   d d i  |  ƒ GHd  S(   Ns       s    (   s   joins   args(   s   args(    (    s   /usr/share/yum/keepalive.pys   DBPRINTv  s    i   s-     fetching the file to establish a connections;     waiting 60 seconds for the server to close the connectioni<   i    s     %2is   s!     fetching the file a second times     data are identicals     ERROR: DATA DIFFER(   s   DBPRINTs   dbps   DEBUGs   urllib2s   urlopens   urls   fos   reads   data1s   closes   is   syss   stdouts   writes   flushs   times   sleeps   stderrs   data2(   s   urls   is   dbps   data2s   data1s   fo(    (    s   /usr/share/yum/keepalive.pys   test_timeouts  s4     	
 
	i
   c         C   sq   d GHy t  |  ƒ Wn$ t j
 o } d GHt i ƒ  n XHd GHt |  ƒ Hd GHt | |  ƒ Hd GHt	 |  ƒ d  S(   Ns,   checking error hander (do this on a non-200)s.   exiting - exception will prevent further testss>   performing continuity test (making sure stuff isn't corrupted)s   performing speed comparisons#   performing dropped-connection check(
   s   error_handlers   urls   IOErrors   es   syss   exits
   continuitys   comps   Ns   test_timeout(   s   urls   Ns   e(    (    s   /usr/share/yum/keepalive.pys   test”  s     
s   __main__s   %s <integer> <url>(   s   __doc__s   urllib2s   httplibs   sockets   VERSIONs   DEBUGs   DBPRINTs   HANDLE_ERRORSs   HTTPHandlers   HTTPResponses   HTTPConnections   error_handlers
   continuitys   comps   fetchs   test_timeouts   tests   __name__s   times   syss   ints   argvs   Ns   url(   s   test_timeouts   error_handlers   HTTPHandlers   tests   HTTPResponses
   continuitys   comps   urllib2s   syss   VERSIONs   HTTPConnections   Ns   httplibs   sockets   urls   times   fetch(    (    s   /usr/share/yum/keepalive.pys   ?4   s2   				rY		!		!		