Pc@swdZddlmZddlZddlZddlmZddlmZddlZej -erej dde nddl Z WdQXyddl mZWn!ek rddlmZnXd d d d d ddddddddddddgZdZdZdZdZdZdZd Zd!Zd"Zd#Zd$Zd%Zd&Zd'Zd(Zd)Z d*Z!d+Z"d,Z#d-Z$d.Z%d/Z&d0Z'd1Z(d2Z)d3Z*d4Z+d5Z,d6Z-d7Z.d8Z/d9Z0d:Z1d;Z2d<Z3d=Z4d>Z5d?Z6d@Z7dAZ8dBZ9dCZ:dDZ;dEZ<dFZ=dGZ>dHZ?dIZ@dJZAdKZBdLZCdMZDdNZEdOZFdPZGi)dQd 6dRd!6dSd#6dTd$6dUd%6dVd&6dWd'6dXd(6dYd)6dZd,6d[d-6d\d.6d]d/6d^d06d_d16d`da6dbd26dcd36ddd46ded56dfd66dgd76dhd86did96djd:6dkd;6dld<6dmd=6dnd>6dod?6dpd@6dqdA6drdB6dsdC6dtdD6dudI6dvdJ6dwdK6dxdL6dydM6dzdN6ZHd{ZId|e jJfd}YZKd fd~YZLd fdYZMd fdYZNyddlOZOWnek rnCXdeMfdYZPejQddeNfdYZRdZSd eTfdYZUd eUfdYZVdeUfdYZWdeUfdYZXdeUfdYZYdeUfdYZZdeUfdYZ[deUfdYZ\de\fdYZ]de\fdYZ^de\fdYZ_deUfdYZ`eUZadfdYZbdZceddkrsecndS(s HTTP/1.1 client library HTTPConnection goes through a number of "states", which define when a client may legally make another request or fetch the response for a particular request. This diagram details these state transitions: (null) | | HTTPConnection() v Idle | | putrequest() v Request-started | | ( putheader() )* endheaders() v Request-sent | | response = getresponse() v Unread-response [Response-headers-read] |\____________________ | | | response.read() | putrequest() v v Idle Req-started-unread-response ______/| / | response.read() | | ( putheader() )* endheaders() v v Request-started Req-sent-unread-response | | response.read() v Request-sent This diagram presents the following rules: -- a second request may not be started until {response-headers-read} -- a response [object] cannot be retrieved until {request-sent} -- there is no differentiation between an unread response body and a partially read response body Note: this enforcement is applied by the HTTPConnection class. The HTTPResponse class does not enforce this state machine, which implies sophisticated clients may accelerate the request/response pipeline. Caution should be taken, though: accelerating the states beyond the above pattern may imply knowledge of the server's connection-close behavior for certain requests. For example, it is impossible to tell whether the server will close the connection UNTIL the response headers have been read; this means that further requests cannot be placed into the pipeline until it is known that the server will NOT be closing the connection. Logical State __state __response ------------- ------- ---------- Idle _CS_IDLE None Request-started _CS_REQ_STARTED None Request-sent _CS_REQ_SENT None Unread-response _CS_IDLE Req-started-unread-response _CS_REQ_STARTED Req-sent-unread-response _CS_REQ_SENT i(tarrayN(t py3kwarning(turlsplittignores.*mimetools has been removed(tStringIOtHTTPt HTTPResponsetHTTPConnectiont HTTPExceptiont NotConnectedtUnknownProtocoltUnknownTransferEncodingtUnimplementedFileModetIncompleteReadt InvalidURLtImproperConnectionStatetCannotSendRequesttCannotSendHeadertResponseNotReadyt BadStatusLineterrort responsesiPitUNKNOWNtIdlesRequest-starteds Request-sentidieifiiiiiiiiii,i-i.i/i0i1i3iiiiiiiiiiiiiiiiiiiiiiiiiiiiiitContinuesSwitching ProtocolstOKtCreatedtAcceptedsNon-Authoritative Informations No Contents Reset ContentsPartial ContentsMultiple ChoicessMoved PermanentlytFounds See Others Not Modifieds Use Proxys(Unused)i2sTemporary Redirects Bad Requestt UnauthorizedsPayment Requiredt Forbiddens Not FoundsMethod Not AllowedsNot AcceptablesProxy Authentication RequiredsRequest TimeouttConflicttGonesLength RequiredsPrecondition FailedsRequest Entity Too LargesRequest-URI Too LongsUnsupported Media TypesRequested Range Not SatisfiablesExpectation FailedsInternal Server ErrorsNot Implementeds Bad GatewaysService UnavailablesGateway TimeoutsHTTP Version Not Supportedit HTTPMessagecBs#eZdZdZdZRS(cCsT|jj|}|dkr.||j| bytes "should" be present for reading. If the bytes are truly not available (due to EOF), then the IncompleteRead exception can be used to detect the problem. iR.(R4Rstmint MAXAMOUNTR R%R;R@(R&RuRvtchunk((s..\python\lib\httplib.pyRtfs cCs |jjS(N(R4tfileno(R&((s..\python\lib\httplib.pyR~scCs.|jdkrtn|jj||S(N(ROR$RRd(R&tnametdefault((s..\python\lib\httplib.pyRds cCs(|jdkrtn|jjS(s&Return list of (header, value) tuples.N(ROR$Rtitems(R&((s..\python\lib\httplib.pyt getheaderss N(RHRIR$RnRZRaRlRfR^RqRsRrRtR~RdR(((s..\python\lib\httplib.pyR@s  % S   $ 6   cBseZdZdZeZeZdZdZ dZ dde j ddZdddZdZdZdZd Zd Zd Zd Zdd ZdddZdZddZdidZdZdZedZRS(i sHTTP/1.1iicCs||_||_d|_g|_d|_t|_d|_d|_ d|_ i|_ |j |||dk r||_ ndS(N(ttimeouttsource_addressR$RWt_buffert_HTTPConnection__responset_CS_IDLEt_HTTPConnection__stateRNt _tunnel_hostt _tunnel_portt_tunnel_headerst _set_hostportRM(R&thosttportRMRR((s..\python\lib\httplib.pyRZs           cCs5||_||_|r$||_n |jjdS(s Sets up the host and the port for the HTTP CONNECT Tunnelling. The headers argument should be a mapping of extra HTTP headers to send with the CONNECT request. N(RRRtclear(R&RRR1((s..\python\lib\httplib.pyt set_tunnels    cCs|dkr|jd}|jd}||kryt||d}Wn)tk rytd||dnX|| }n |j}|r|ddkr|ddkr|dd!}qn||_||_dS(Nt:t]isnonnumeric port: '%s'it[i(R$trfindR`R]Rt default_portRR(R&RRRztj((s..\python\lib\httplib.pyRs     & cCs ||_dS(N(RL(R&tlevel((s..\python\lib\httplib.pytset_debuglevelscCs|j|j|j|jd|j|jfx4|jjD]#\}}|jd||fqCW|jd|j|j d|j d|j }|j \}}}|dkr|j tjd||jfnx)tr|jj}|dkrPqqWdS(NsCONNECT %s:%d HTTP/1.0 s%s: %s s RMRXisTunnel connection failed: %d %s(RRRtsendRRRt iteritemstresponse_classRWRMRNRaR^tsocketRR<R7R4R9(R&theaderR(tresponseRQtcodetmessageRF((s..\python\lib\httplib.pyt_tunnels        cCsDtj|j|jf|j|j|_|jr@|jndS(s3Connect to the host and port specified in __init__.N( Rtcreate_connectionRRRRRWRR(R&((s..\python\lib\httplib.pytconnects cCsQ|jr"|jjd|_n|jrD|jjd|_nt|_dS(s(Close the connection to the HTTP server.N(RWR^R$RRR(R&((s..\python\lib\httplib.pyR^s      cCs|jdkr1|jr%|jq1tn|jdkrRdGt|GHnd}t|drt|t  r|jdkrdGHn|j |}x<|r|jj ||j |}qWn|jj |dS(sSend `data' to the server.issend:i RsssendIng a read()ableN( RWR$t auto_openRR RLR[R3t isinstanceRRstsendall(R&tdatat blocksizet datablock((s..\python\lib\httplib.pyRs    cCs|jj|dS(suAdd a line of output to the current request buffer. Assumes that the line does *not* end with \r\n. N(RR;(R&Rv((s..\python\lib\httplib.pyt_outputscCsx|jjddj|j}|j2t|trK||7}d}n|j||dk rt|j|ndS(sSend the currently buffered request and clear the buffer. Appends an extra \r\n to the buffer. A message_body may be specified, to be appended to the request. R.s N(R.R.(RtextendR%RtstrR$R(R&t message_bodyRO((s..\python\lib\httplib.pyt _send_outputs    c Cs|jr$|jjr$d|_n|jtkr?t|_n t||_|s`d}nd|||jf}|j ||j dkr|sd}|j drt |\}}}}}n|ry|j d}Wn tk r|j d}nX|jd|qy|jj d} Wn#tk rU|jj d} nX| jd d kr|d | d } n|j|jkr|jd| q|jdd | |jfn|s|jddqndS(s`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 t/s%s %s %si R.thttptasciitidnatHostRiRRs%s:%ssAccept-EncodingtidentityN(RRqR$RRt_CS_REQ_STARTEDRRNt _http_vsn_strRt _http_vsnR:RtencodetUnicodeEncodeErrort putheaderRRyRR( R&RXturlt skip_hosttskip_accept_encodingRjtnetloctnilt netloc_encthost_enc((s..\python\lib\httplib.pyt putrequest#sB           cGs^|jtkrtnd|djg|D]}t|^q.f}|j|dS(skSend a request header line to the server. For example: h.putheader('Accept', 'text/html') s%s: %ss N(RRRR%RR(R&RtvaluestvRj((s..\python\lib\httplib.pyRs 2cCs5|jtkrt|_n t|j|dS(s|Indicate that the last header line has been sent to the server. This method sends the request to the server. The optional message_body argument can be used to pass message body associated with the request. The message body will be sent in the same packet as the message headers if possible. The message_body should be a string. N(RRt _CS_REQ_SENTRR(R&R((s..\python\lib\httplib.pyt endheaderss   cCs|j||||dS(s&Send a complete request to the server.N(t _send_request(R&RXRtbodyR1((s..\python\lib\httplib.pytrequestscCsd}ytt|}Wnftk r}y"ttj|jj}Wqtt fk r|j dkrdGHqqXnX|dk r|j d|ndS(Nis Cannot stat!!sContent-Length( R$RR@t TypeErrortostfstatR~tst_sizetAttributeErrortOSErrorRLR(R&Rtthelentte((s..\python\lib\httplib.pyt_set_content_lengths" c Cstjg|D]}|j^q }i}d|krGd|dCs,            iRs[   D A