mò óRÅDc@sedZdkZdkZdkZdkZdadkZeidjo da nda dfd„ƒYZ dei fd „ƒYZ d ei fd „ƒYZ d ei fd „ƒYZ d„Zd„Zd„Zdd„Zd„Zdd„ZedjoadkZdkZy$eeidƒZeidZWndeidGHqaXeeeƒndS(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. NiiiitConnectionManagercBsGtZdZd„Zd„Zd„Zd„Zd„Zed„Z RS(sV The connection manager must be able to: * keep track of all existing cCs.tiƒ|_h|_h|_h|_dS(N(tthreadt allocate_locktselft_lockt_hostmapt_connmapt _readymap(R((t7/usr/lib/python2.4/site-packages/mercurial/keepalive.pyt__init__xs  cCsx|iiƒzV|ii|ƒpg|i| 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(RR!RR thRtclose(RR R'((Rtclose_connection¹s cCsUxN|iiƒiƒD]7\}}x(|D] }|ii|ƒ|iƒq)WqWdS(sclose all open connectionsN( RR!RR#R tconnsR'RR((RR*R'R ((Rt close_allÁs cCs|ii|dƒdS(sdtells us that this request is now closed and the the connection is ready for another requestiN(RR!RR(RtrequestR R((Rt_request_closedÈsicCs)|o|iƒn|ii|ƒdS(N(R(RRR!R(RR RR(((Rt_remove_connectionÍscCs|it|ƒS(N(Rtdo_opentHTTPConnectiontreq(RR1((Rt http_openÒscCs |iƒ}|ptidƒ‚nyÔ|ii|ƒ}x»|oQ|i |||ƒ}|oPn|i ƒ|ii |ƒ|ii|ƒ}q>W||ƒ}totid|t|ƒƒn|ii||dƒ|i||ƒ|iƒ}Wn0titifj o}ti|ƒ‚nX|io|ii |ƒntotid|i|iƒn||_||_|iƒ|_||_ |i|_!|i"|_#|i|_"|idjpt$ o|Sn)|i%id|||i|i"|i#ƒSdS(Ns no host givens"creating new connection to %s (%d)isSTATUS: %s, %siÈthttp(&R1tget_hostR turllib2tURLErrorRR!RR't_reuse_connectiontrR(Rt http_classtDEBUGtinfotidRt_start_transactiont getresponsetsocketterrorthttplibt HTTPExceptionterrt will_closetstatustreasont_handlert_hostt get_full_urlt_urlt _connectiontcodetmsgtheaderst HANDLE_ERRORStparent(RR9R1RCR'R R8((RR/ÕsJ           cCsy |i||ƒ|iƒ}Wnptiti fj o d}nMt o!t idd|t |ƒƒn|ii|ƒ|iƒ‚nX|djp|idjo.t ot id|t |ƒƒnd}n%t ot id|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. sunexpected exception - closing sconnection to %s (%d)i s&failed to re-use connection to %s (%d)sre-using connection to %s (%d)N(RR=R'R1R>R8R?R@RARBRR:R R<R!RR(tversionR;(RR'R1R R8((RR7s*      cCsXy¥|iƒo~|iƒ}|id|iƒƒ|iidƒp|i ddƒn|iidƒp|i ddt |ƒƒq¤n|id|iƒƒWn0t i t ifj o}ti|ƒ‚nXx!|iiD]}|i |ŒqåWx-|iiƒD]\}}|i ||ƒq W|iƒ|iƒo|i|ƒndS(NtPOSTs Content-types!application/x-www-form-urlencodedsContent-lengths%dtGET(R1thas_datatget_datatdataR't putrequestt get_selectorRNR t putheaderR%R?R@RARBRCR5R6RRPt addheaderstargsR#tktvt endheaderstsend(RR'R1R[RCR]RVR\((RR=0s*    "    ( RRR R&R)R+R-R.R2R/R7R=(((RR ¯s        0 +t HTTPResponsecBsntZdddd„ZeiiZd„Zd„Z d„Z d„Z dd„Zdd „Z dd „Z RS( NicCs‰|otii||||ƒntii|||ƒ|i|_d|_ d|_ d|_ d|_ d|_ d|_d|_dS(Nti (tmethodRAR`R Rtsockt debugleveltfilenoRRLt_rbuft _rbufsizeRGRHRJRK(RRcRdtstrictRb((RR [s       cCsR|ioD|iiƒd|_|io |ii||i|iƒqNndS(N(RtfpR(RRGR-RHRK(R((RR(ks     cCs-|ii|i|iddƒ|iƒdS(NR(i(RRGR.RHRKR((R((RR)sscCs|iS(N(RRN(R((RR;wscCs|iS(N(RRJ(R((RtgeturlzscCs‰|io\|dj oOt|iƒ}||jo||8}qf|i| }|i||_|Sn|i|i|ƒ}d|_|S(NRa(RRftamtRR%tLtst _raw_read(RRkRlRm((Rtread}s   iÿÿÿÿcCs3d}|iidƒ}x›|djod|jot|iƒjn oe|i|iƒ}|pPn|idƒ}|djo|t|iƒ}n|i||_qW|djot|iƒ}n |d}d|jot|iƒjno |}n|i| |i|}|_|S(NRas ii( RVRRftfindtitlimitR%RnRgtnew(RRrRqRsRV((Rtreadlines$5   ' cCsfd}g}xS|iƒ}|pPn|i|ƒ|t|ƒ7}|o||joPqqW|S(Nii(ttotalRRRttlineR R%tsizehint(RRwRRvRu((Rt readlinesœs   (RRRR RAR`RoRnR(R)R;RjRtRx(((RR`Gs       R0cBstZeZRS(N(RRR`tresponse_class(((RR0©sc Cs+t} tƒ} ti| ƒ}ti|ƒhdd<dd<}x¼d D]´}d|||fGH|aybti |ƒ} | i ƒ}| iƒy| i| i}}Wntj od \}}nXWn!tj o}d|GH‚qJXd||fGHqJW| a| iƒ}dG|GH| iƒdS( Nitoffitons. fancy error handling %s (HANDLE_ERRORS = %i)s EXCEPTION: %ss status = %s, reason = %ssopen connections:(ii(NN(ROtorigR tkeepalive_handlerR5t build_openertopenertinstall_openertposRqturlopenturltfoRotfooR(RERFtAttributeErrorRtIOErrorteR&thostsR+( RƒRER…RˆRqRFRRR‰R„R}R|((Rt error_handler±s4       cCsJdk}d}tiƒ}ti|ƒti|ƒ}|i ƒ}|i ƒ|i |ƒ}|d|iƒfGHtitƒƒ}ti|ƒti|ƒ}|i ƒ}|i ƒ|i |ƒ}|d|iƒfGHti|ƒ}d}x'|iƒ}|o||}qñPqñW|i ƒ|i |ƒ}|d|iƒfGHdS(Ns%25s: %ss normal urllibskeepalive readRaiskeepalive readline(tmd5tformatR5R~RR€R‚RƒR„RoR…R(Rstmt hexdigestR Rttf(RƒR…RŒRRRR‹R„((Rt continuityËs6          cCs«d||fGHtiidƒtiƒ}ti|ƒt ||ƒ}d|GHtiidƒtit ƒƒ}ti|ƒt ||ƒ}d|GHd||fGHdS(Ns making %i connections to: %ss( first using the normal urllib handlerss TIME: %.3f ss( now using the keepalive handler s improvement factor: %.2f( tNRƒtsyststdouttwriteR5R~RR€tfetchtt1R tt2(R‘RƒR—R–R((Rtcompìs     c Csìdk}g}|iƒ}xqt|ƒD]c}|o|djo|i|ƒnti |ƒ} | i ƒ} | iƒ|it| ƒƒq(W|iƒ|}d}x@|dD]4}|d}||djpd||fGHq°q°W|S(Niis+WARNING: inconsistent length on read %i: %i(ttimetlenst starttimetrangeR‘RqtdelaytsleepR5R‚RƒR„RoR…R(R R%tdifftj( R‘RƒRRšR RqR›R™RŸR…R„((RR•þs&       cCst}dfd„ƒY}|ƒadGHti|ƒ}|iƒ}|i ƒd}d|GHxJ|djo<t i i d|ƒt i iƒtidƒ|d8}q^Wt ii d ƒd GHti|ƒ}|iƒ}|i ƒ||jo d GHnd GH|adS( Nt FakeLoggercBstZd„ZeZZZRS(NcGs ||GHdS(N(RMR[(RRMR[((Rtdebugs(RRR¢R;twarningR@(((RR¡s 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(R:tdbbackupR¡R5R‚RƒR„Rotdata1R(RqR’R“R”tflushR™Ržtstderrtdata2(RƒRqR¥R¡R¨R¤R„((Rt test_timeouts2           i cCsqdGHyt|ƒWn$tj o}dGHtiƒnXHdGHt|ƒHdGHt||ƒHdGHt |ƒdS(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)sperforming speed comparisons#performing dropped-connection check( RŠRƒR‡RˆR’texitRR˜R‘R©(RƒR‘Rˆ((Rttest4s  t__main__s%s (ii(RR5RAR?RRR:R’t version_infoRORR R`R0RŠRR˜R•R©R«RR™tinttargvR‘Rƒ(R©RRŠR R«R`RR˜R5R’R0R‘RAR?RRƒR™R•((Rt?ds8      <˜b  !   "