\x89\x50\x4E\x47\x0D\x0A\x1A\x0A PNG  \x89\x50\x4E\x47\x0D\x0A\x1A\x0A  13\c@@sdZddlmZmZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z mZmZddlmZejZejZejZdadefd YZd efd YZd eejfd YZdejfdYZdZdZdejfdYZdZ dZ!ddZ"dZ#ddZ$e%dkrddl&Z&y$e'ej(dZ)ej(dZ*Wn,e+e,fk re-dej(dqXe$e*e)ndS(sKAn HTTP handler for urllib2 that supports HTTP 1.1 and keepalive. >>> import urllib2 >>> from keepalive import HTTPHandler >>> keepalive_handler = HTTPHandler() >>> opener = urlreq.buildopener(keepalive_handler) >>> urlreq.installopener(opener) >>> >>> fo = urlreq.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 (i.e. 404) reason - english translation of status (i.e. '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. i(tabsolute_importtprint_functionNi(t_(tnodetpycompatt urllibcompattutil(tprocutiltConnectionManagercB@sGeZdZdZdZdZdZdZddZ RS(sV The connection manager must be able to: * keep track of all existing cC@s.tj|_i|_i|_i|_dS(N(t threadingtLockt_lockt_hostmapt_connmapt _readymap(tself((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyt__init__ss  cC@ss|jjzQ||jkr/g|j| 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.N(R'R"Rtclose(RRth((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pytclose_connectionscC@sUxN|jjjD]7\}}x(|D] }|jj||jq)WqWdS(sclose all open connectionsN(R'R"t iteritemsRR0(RRtconnsR1((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyt close_alls" cC@s|jj|ddS(setells us that this request is now closed and that the connection is ready for another requestiN(R'R(RtrequestRR((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyt_request_closedsicC@s'|r|jn|jj|dS(N(R0R'R(RRRR0((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyt_remove_connections cC@s|jt|S(N(tdo_opentHTTPConnection(Rtreq((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyt http_openscC@stj|}|s'tjdny|jj|}x|r|j|||}|rdPn|j|jj||jj|}q?W||d|j }t rt j d|t |n|jj ||d|j|||j}Wnhtjk r;}tjtdtj|jn.tjtjfk rh}tj|nXt|dtr|jj|nt rt j d|j|jn||_||_|j|_ ||_!|j|_"|j#|_$|j|_#|S(Ns no host givenR+s"creating new connection to %s (%d)isbad HTTP status line: %st will_closesSTATUS: %s, %s(%RtgethostturlerrturlerrorR'Rt_reuse_connectionR0RR(tDEBUGtinfotidRt_start_transactiont getresponsethttplibt BadStatusLineRRtsysbytestlinetsocketterrort HTTPExceptiontgetattrtTruetstatustreasont_handlert_hostt get_full_urlt_urlt _connectiontcodetmsgtheaders(Rt http_classR;RR1trterr((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyR9sH   "      cC@sy |j|||j}Wnhtjtjfk rEd}nFtrjtjd|t|n|j j ||j nX|dks|j dkrtrtj d|t|nd}n"trtj d|t|n|S(sGstart the transaction with a re-used connection return a response object (r) upon success or None on failure. This DOES not close or remove bad connections in cases where it returns. However, if an unexpected exception occurs, it will close and remove the connection before re-raising. s4unexpected exception - closing connection to %s (%d)i s&failed to re-use connection to %s (%d)sre-using connection to %s (%d)N(RERFRKRLRGRMRRBRDR'RR0tversionRC(RR1R;RR[((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyRAs(      c C@skt|dd}tj|jj}|jt|jj|jt|j jtjd|jD}i}x7dD]/}||krd|d|j dd FsRsaccept-encodingitskip_t-Rs content-types Content-types!application/x-www-form-urlencodedscontent-lengthsContent-lengths%d(Rsaccept-encoding(RNRtsortdicttparentt addheaderstupdatetsortedRYR,tunredirected_hdrstreplaceRthasdatatgetdatat putrequestt get_methodt getselectort putheaderR-RKRLR?R@t endheaderstsendR)R*tAttributeError( RR1R;t oldbytescountRYt skipheadersR`tdataR\tkRa((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyRE=sL  !         N( R#R$RRR/R2R5R7R8R<R9RARE(((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyR&s        7 .t HTTPHandlercB@seZRS((R#R$(((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyRxmst HTTPResponsecB@seZddd dZejjZeejdd Z dZ dZ dZ dZ d dZdZd Zdd Zd ZRS( icC@si}tjs&t|d           c@s"fd}jj|_|S(s>Wraps getresponse in cls with a broken-pipe sane version. c@s/t|dd}|dk r"|Sj|S(NR(RNRRF(RR[(tcls(s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pytsafegetresponses (RFR%(RR((Rs9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pytwrapgetresponses R:cB@s,eZeZeZeejZ dZ RS(cO@s,tjj|||d|_d|_dS(Ni(RGR:RR*R(Rtargstkwargs((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyRs ( R#R$Rytresponse_classRRrRRGR:RFR(((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyR:scC@sntj}d}tj}tj|tj|}|j}|j||}t|dt j |j ftjt }tj|tj|}|j}|j||}t|dt j |j ftj|}d}x*t r0|j}|r,||}qPqW|j||}t|dt j |j fdS(Ns%25s: %ss normal urllibskeepalive readR~skeepalive readline(thashlibtmd5turlreqt buildopenert installopenerturlopenRR0RRthextdigestRxROR(turlRtformattopenertfotfootmtf((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyt continuitys4      #    #     cC@std||ftjjdtj}tj|t||}td|tjjdtjt}tj|t||}td|td||dS(Ns making %i connections to: %ss( first using the normal urllib handlerss TIME: %.3f ss( now using the keepalive handler s improvement factor: %.2f( RRtstdouttwriteRRRtfetchRx(tNRRtt1tt2((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pytcomps   c C@sddl}g}|j}xnt|D]`}|rS|dkrS|j|ntj|}|j}|j|jt|q+W|j|} d} xC|dD]7}| d} ||dkst d| |fqqW| S(Niis+WARNING: inconsistent length on read %i: %i( ttimetrangetsleepRRRR0RR-R( RRtdelayRtlenst starttimeRRRtdifftj((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyRs"     cC@st}dtfdY}|atdtj|}|j}|jd}td|xH|dkrtjj d|tjj t j d|d8}qkWtj j d td tj|}|j}|j||kr td n td |adS( Nt FakeLoggercB@seZdZeZZZRS(cW@st||dS(N(R(RRXR((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pytdebugs(R#R$RRCtwarningRL(((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyRs s- fetching the file to establish a connectionis; waiting %i seconds for the server to close the connectionis %2iis s! fetching the file a second times data are identicals ERROR: DATA DIFFER(RBtobjectRRRRR0RRRtflushRRtstderr(RtdbbackupRRtdata1Rtdata2((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyt test_timeouts.            i cC@sWtdt|tdtdt||tdtdt|dS(Ns>performing continuity test (making sure stuff isn't corrupted)R~sperforming speed comparisons#performing dropped-connection check(RRRR(RR((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pyttests       t__main__is%s (.R%t __future__RRRRRKtsysR ti18nRR~RRRRtutilsRRGR?RRRBRRR&t httphandlerRxRyRRR:RRRRRR#RRtargvRRt IndexErrorRR(((s9/usr/lib64/python2.7/site-packages/mercurial/keepalive.pytQsB     "   @ 5  #   #