<s> """<STR_LIT>""" <EOL> import errno <EOL> import mimetools <EOL> import socket <EOL> from urlparse import urlsplit <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:error>" , "<STR_LIT>" ] <EOL> HTTP_PORT = <NUM_LIT> <EOL> HTTPS_PORT = <NUM_LIT> <EOL> _UNKNOWN = '<STR_LIT>' <EOL> _CS_IDLE = '<STR_LIT>' <EOL> _CS_REQ_STARTED = '<STR_LIT>' <EOL> _CS_REQ_SENT = '<STR_LIT>' <EOL> CONTINUE = <NUM_LIT:100> <EOL> SWITCHING_PROTOCOLS = <NUM_LIT> <EOL> PROCESSING = <NUM_LIT> <EOL> OK = <NUM_LIT:200> <EOL> CREATED = <NUM_LIT> <EOL> ACCEPTED = <NUM_LIT> <EOL> NON_AUTHORITATIVE_INFORMATION = <NUM_LIT> <EOL> NO_CONTENT = <NUM_LIT> <EOL> RESET_CONTENT = <NUM_LIT> <EOL> PARTIAL_CONTENT = <NUM_LIT> <EOL> MULTI_STATUS = <NUM_LIT> <EOL> IM_USED = <NUM_LIT> <EOL> MULTIPLE_CHOICES = <NUM_LIT> <EOL> MOVED_PERMANENTLY = <NUM_LIT> <EOL> FOUND = <NUM_LIT> <EOL> SEE_OTHER = <NUM_LIT> <EOL> NOT_MODIFIED = <NUM_LIT> <EOL> USE_PROXY = <NUM_LIT> <EOL> TEMPORARY_REDIRECT = <NUM_LIT> <EOL> BAD_REQUEST = <NUM_LIT> <EOL> UNAUTHORIZED = <NUM_LIT> <EOL> PAYMENT_REQUIRED = <NUM_LIT> <EOL> FORBIDDEN = <NUM_LIT> <EOL> NOT_FOUND = <NUM_LIT> <EOL> METHOD_NOT_ALLOWED = <NUM_LIT> <EOL> NOT_ACCEPTABLE = <NUM_LIT> <EOL> PROXY_AUTHENTICATION_REQUIRED = <NUM_LIT> <EOL> REQUEST_TIMEOUT = <NUM_LIT> <EOL> CONFLICT = <NUM_LIT> <EOL> GONE = <NUM_LIT> <EOL> LENGTH_REQUIRED = <NUM_LIT> <EOL> PRECONDITION_FAILED = <NUM_LIT> <EOL> REQUEST_ENTITY_TOO_LARGE = <NUM_LIT> <EOL> REQUEST_URI_TOO_LONG = <NUM_LIT> <EOL> UNSUPPORTED_MEDIA_TYPE = <NUM_LIT> <EOL> REQUESTED_RANGE_NOT_SATISFIABLE = <NUM_LIT> <EOL> EXPECTATION_FAILED = <NUM_LIT> <EOL> UNPROCESSABLE_ENTITY = <NUM_LIT> <EOL> LOCKED = <NUM_LIT> <EOL> FAILED_DEPENDENCY = <NUM_LIT> <EOL> UPGRADE_REQUIRED = <NUM_LIT> <EOL> INTERNAL_SERVER_ERROR = <NUM_LIT> <EOL> NOT_IMPLEMENTED = <NUM_LIT> <EOL> BAD_GATEWAY = <NUM_LIT> <EOL> SERVICE_UNAVAILABLE = <NUM_LIT> <EOL> GATEWAY_TIMEOUT = <NUM_LIT> <EOL> HTTP_VERSION_NOT_SUPPORTED = <NUM_LIT> <EOL> INSUFFICIENT_STORAGE = <NUM_LIT> <EOL> NOT_EXTENDED = <NUM_LIT> <EOL> responses = { <EOL> <NUM_LIT:100> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT:200> : '<STR_LIT:OK>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> } <EOL> MAXAMOUNT = <NUM_LIT> <EOL> class HTTPMessage ( mimetools . Message ) : <EOL> def addheader ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> prev = self . dict . get ( key ) <EOL> if prev is None : <EOL> self . dict [ key ] = value <EOL> else : <EOL> combined = "<STR_LIT:U+002CU+0020>" . join ( ( prev , value ) ) <EOL> self . dict [ key ] = combined <EOL> def addcontinue ( self , key , more ) : <EOL> """<STR_LIT>""" <EOL> prev = self . dict [ key ] <EOL> self . dict [ key ] = prev + "<STR_LIT>" + more <EOL> def readheaders ( self ) : <EOL> """<STR_LIT>""" <EOL> self . dict = { } <EOL> self . unixfrom = '<STR_LIT>' <EOL> self . headers = hlist = [ ] <EOL> self . status = '<STR_LIT>' <EOL> headerseen = "<STR_LIT>" <EOL> firstline = <NUM_LIT:1> <EOL> startofline = unread = tell = None <EOL> if hasattr ( self . fp , '<STR_LIT>' ) : <EOL> unread = self . fp . unread <EOL> elif self . seekable : <EOL> tell = self . fp . tell <EOL> while True : <EOL> if tell : <EOL> try : <EOL> startofline = tell ( ) <EOL> except IOError : <EOL> startofline = tell = None <EOL> self . seekable = <NUM_LIT:0> <EOL> line = self . fp . readline ( ) <EOL> if not line : <EOL> self . status = '<STR_LIT>' <EOL> break <EOL> if firstline and line . startswith ( '<STR_LIT>' ) : <EOL> self . unixfrom = self . unixfrom + line <EOL> continue <EOL> firstline = <NUM_LIT:0> <EOL> if headerseen and line [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> hlist . append ( line ) <EOL> self . addcontinue ( headerseen , line . strip ( ) ) <EOL> continue <EOL> elif self . iscomment ( line ) : <EOL> continue <EOL> elif self . islast ( line ) : <EOL> break <EOL> headerseen = self . isheader ( line ) <EOL> if headerseen : <EOL> hlist . append ( line ) <EOL> self . addheader ( headerseen , line [ len ( headerseen ) + <NUM_LIT:1> : ] . strip ( ) ) <EOL> continue <EOL> else : <EOL> if not self . dict : <EOL> self . status = '<STR_LIT>' <EOL> else : <EOL> self . status = '<STR_LIT>' <EOL> if unread : <EOL> unread ( line ) <EOL> elif tell : <EOL> self . fp . seek ( startofline ) <EOL> else : <EOL> self . status = self . status + '<STR_LIT>' <EOL> break <EOL> class HTTPResponse : <EOL> def __init__ ( self , sock , debuglevel = <NUM_LIT:0> , strict = <NUM_LIT:0> , method = None ) : <EOL> self . fp = sock . makefile ( '<STR_LIT:rb>' , <NUM_LIT:0> ) <EOL> self . debuglevel = debuglevel <EOL> self . strict = strict <EOL> self . _method = method <EOL> self . msg = None <EOL> self . version = _UNKNOWN <EOL> self . status = _UNKNOWN <EOL> self . reason = _UNKNOWN <EOL> self . chunked = _UNKNOWN <EOL> self . chunk_left = _UNKNOWN <EOL> self . length = _UNKNOWN <EOL> self . will_close = _UNKNOWN <EOL> def _read_status ( self ) : <EOL> line = self . fp . readline ( ) <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> print "<STR_LIT>" , repr ( line ) <EOL> if not line : <EOL> raise BadStatusLine ( line ) <EOL> try : <EOL> [ version , status , reason ] = line . split ( None , <NUM_LIT:2> ) <EOL> except ValueError : <EOL> try : <EOL> [ version , status ] = line . split ( None , <NUM_LIT:1> ) <EOL> reason = "<STR_LIT>" <EOL> except ValueError : <EOL> version = "<STR_LIT>" <EOL> if not version . startswith ( '<STR_LIT>' ) : <EOL> if self . strict : <EOL> self . close ( ) <EOL> raise BadStatusLine ( line ) <EOL> else : <EOL> self . fp = LineAndFileWrapper ( line , self . fp ) <EOL> return "<STR_LIT>" , <NUM_LIT:200> , "<STR_LIT>" <EOL> try : <EOL> status = int ( status ) <EOL> if status < <NUM_LIT:100> or status > <NUM_LIT> : <EOL> raise BadStatusLine ( line ) <EOL> except ValueError : <EOL> raise BadStatusLine ( line ) <EOL> return version , status , reason <EOL> def begin ( self ) : <EOL> if self . msg is not None : <EOL> return <EOL> while True : <EOL> version , status , reason = self . _read_status ( ) <EOL> if status != CONTINUE : <EOL> break <EOL> while True : <EOL> skip = self . fp . readline ( ) . strip ( ) <EOL> if not skip : <EOL> break <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> print "<STR_LIT>" , skip <EOL> self . status = status <EOL> self . reason = reason . strip ( ) <EOL> if version == '<STR_LIT>' : <EOL> self . version = <NUM_LIT:10> <EOL> elif version . startswith ( '<STR_LIT>' ) : <EOL> self . version = <NUM_LIT:11> <EOL> elif version == '<STR_LIT>' : <EOL> self . version = <NUM_LIT:9> <EOL> else : <EOL> raise UnknownProtocol ( version ) <EOL> if self . version == <NUM_LIT:9> : <EOL> self . length = None <EOL> self . chunked = <NUM_LIT:0> <EOL> self . will_close = <NUM_LIT:1> <EOL> self . msg = HTTPMessage ( StringIO ( ) ) <EOL> return <EOL> self . msg = HTTPMessage ( self . fp , <NUM_LIT:0> ) <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> for hdr in self . msg . headers : <EOL> print "<STR_LIT>" , hdr , <EOL> self . msg . fp = None <EOL> tr_enc = self . msg . getheader ( '<STR_LIT>' ) <EOL> if tr_enc and tr_enc . lower ( ) == "<STR_LIT>" : <EOL> self . chunked = <NUM_LIT:1> <EOL> self . chunk_left = None <EOL> else : <EOL> self . chunked = <NUM_LIT:0> <EOL> self . will_close = self . _check_close ( ) <EOL> length = self . msg . getheader ( '<STR_LIT>' ) <EOL> if length and not self . chunked : <EOL> try : <EOL> self . length = int ( length ) <EOL> except ValueError : <EOL> self . length = None <EOL> else : <EOL> self . length = None <EOL> if ( status == NO_CONTENT or status == NOT_MODIFIED or <EOL> <NUM_LIT:100> <= status < <NUM_LIT:200> or <EOL> self . _method == '<STR_LIT>' ) : <EOL> self . length = <NUM_LIT:0> <EOL> if not self . will_close and not self . chunked and self . length is None : <EOL> self . will_close = <NUM_LIT:1> <EOL> def _check_close ( self ) : <EOL> conn = self . msg . getheader ( '<STR_LIT>' ) <EOL> if self . version == <NUM_LIT:11> : <EOL> conn = self . msg . getheader ( '<STR_LIT>' ) <EOL> if conn and "<STR_LIT>" in conn . lower ( ) : <EOL> return True <EOL> return False <EOL> if self . msg . getheader ( '<STR_LIT>' ) : <EOL> return False <EOL> if conn and "<STR_LIT>" in conn . lower ( ) : <EOL> return False <EOL> pconn = self . msg . getheader ( '<STR_LIT>' ) <EOL> if pconn and "<STR_LIT>" in pconn . lower ( ) : <EOL> return False <EOL> return True <EOL> def close ( self ) : <EOL> if self . fp : <EOL> self . fp . close ( ) <EOL> self . fp = None <EOL> def isclosed ( self ) : <EOL> return self . fp is None <EOL> def read ( self , amt = None ) : <EOL> if self . fp is None : <EOL> return '<STR_LIT>' <EOL> if self . chunked : <EOL> return self . _read_chunked ( amt ) <EOL> if amt is None : <EOL> if self . length is None : <EOL> s = self . fp . read ( ) <EOL> else : <EOL> s = self . _safe_read ( self . length ) <EOL> self . length = <NUM_LIT:0> <EOL> self . close ( ) <EOL> return s <EOL> if self . length is not None : <EOL> if amt > self . length : <EOL> amt = self . length <EOL> s = self . fp . read ( amt ) <EOL> if self . length is not None : <EOL> self . length -= len ( s ) <EOL> return s <EOL> def _read_chunked ( self , amt ) : <EOL> assert self . chunked != _UNKNOWN <EOL> chunk_left = self . chunk_left <EOL> value = '<STR_LIT>' <EOL> while True : <EOL> if chunk_left is None : <EOL> line = self . fp . readline ( ) <EOL> i = line . find ( '<STR_LIT:;>' ) <EOL> if i >= <NUM_LIT:0> : <EOL> line = line [ : i ] <EOL> chunk_left = int ( line , <NUM_LIT:16> ) <EOL> if chunk_left == <NUM_LIT:0> : <EOL> break <EOL> if amt is None : <EOL> value += self . _safe_read ( chunk_left ) <EOL> elif amt < chunk_left : <EOL> value += self . _safe_read ( amt ) <EOL> self . chunk_left = chunk_left - amt <EOL> return value <EOL> elif amt == chunk_left : <EOL> value += self . _safe_read ( amt ) <EOL> self . _safe_read ( <NUM_LIT:2> ) <EOL> self . chunk_left = None <EOL> return value <EOL> else : <EOL> value += self . _safe_read ( chunk_left ) <EOL> amt -= chunk_left <EOL> self . _safe_read ( <NUM_LIT:2> ) <EOL> chunk_left = None <EOL> while True : <EOL> line = self . fp . readline ( ) <EOL> if not line : <EOL> break <EOL> if line == '<STR_LIT:\r\n>' : <EOL> break <EOL> self . close ( ) <EOL> return value <EOL> def _safe_read ( self , amt ) : <EOL> """<STR_LIT>""" <EOL> s = [ ] <EOL> while amt > <NUM_LIT:0> : <EOL> chunk = self . fp . read ( min ( amt , MAXAMOUNT ) ) <EOL> if not chunk : <EOL> raise IncompleteRead ( s ) <EOL> s . append ( chunk ) <EOL> amt -= len ( chunk ) <EOL> return '<STR_LIT>' . join ( s ) <EOL> def getheader ( self , name , default = None ) : <EOL> if self . msg is None : <EOL> raise ResponseNotReady ( ) <EOL> return self . msg . getheader ( name , default ) <EOL> def getheaders ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . msg is None : <EOL> raise ResponseNotReady ( ) <EOL> return self . msg . items ( ) <EOL> class HTTPConnection : <EOL> _http_vsn = <NUM_LIT:11> <EOL> _http_vsn_str = '<STR_LIT>' <EOL> response_class = HTTPResponse <EOL> default_port = HTTP_PORT <EOL> auto_open = <NUM_LIT:1> <EOL> debuglevel = <NUM_LIT:0> <EOL> strict = <NUM_LIT:0> <EOL> def __init__ ( self , host , port = None , strict = None ) : <EOL> self . sock = None <EOL> self . _buffer = [ ] <EOL> self . __response = None <EOL> self . __state = _CS_IDLE <EOL> self . _method = None <EOL> self . _set_hostport ( host , port ) <EOL> if strict is not None : <EOL> self . strict = strict <EOL> def _set_hostport ( self , host , port ) : <EOL> if port is None : <EOL> i = host . rfind ( '<STR_LIT::>' ) <EOL> j = host . rfind ( '<STR_LIT:]>' ) <EOL> if i > j : <EOL> try : <EOL> port = int ( host [ i + <NUM_LIT:1> : ] ) <EOL> except ValueError : <EOL> raise InvalidURL ( "<STR_LIT>" % host [ i + <NUM_LIT:1> : ] ) <EOL> host = host [ : i ] <EOL> else : <EOL> port = self . default_port <EOL> if host and host [ <NUM_LIT:0> ] == '<STR_LIT:[>' and host [ - <NUM_LIT:1> ] == '<STR_LIT:]>' : <EOL> host = host [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> self . host = host <EOL> self . port = port <EOL> def set_debuglevel ( self , level ) : <EOL> self . debuglevel = level <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = "<STR_LIT>" <EOL> for res in socket . getaddrinfo ( self . host , self . port , <NUM_LIT:0> , <EOL> socket . SOCK_STREAM ) : <EOL> af , socktype , proto , canonname , sa = res <EOL> try : <EOL> self . sock = socket . socket ( af , socktype , proto ) <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> print "<STR_LIT>" % ( self . host , self . port ) <EOL> self . sock . connect ( sa ) <EOL> except socket . error , msg : <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> print '<STR_LIT>' , ( self . host , self . port ) <EOL> if self . sock : <EOL> self . sock . close ( ) <EOL> self . sock = None <EOL> continue <EOL> break <EOL> if not self . sock : <EOL> raise socket . error , msg <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . sock : <EOL> self . sock . close ( ) <EOL> self . sock = None <EOL> if self . __response : <EOL> self . __response . close ( ) <EOL> self . __response = None <EOL> self . __state = _CS_IDLE <EOL> def send ( self , str ) : <EOL> """<STR_LIT>""" <EOL> if self . sock is None : <EOL> if self . auto_open : <EOL> self . connect ( ) <EOL> else : <EOL> raise NotConnected ( ) <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> print "<STR_LIT>" , repr ( str ) <EOL> try : <EOL> self . sock . sendall ( str ) <EOL> except socket . error , v : <EOL> if v [ <NUM_LIT:0> ] == <NUM_LIT:32> : <EOL> self . close ( ) <EOL> raise <EOL> def _output ( self , s ) : <EOL> """<STR_LIT>""" <EOL> self . _buffer . append ( s ) <EOL> def _send_output ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _buffer . extend ( ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> msg = "<STR_LIT:\r\n>" . join ( self . _buffer ) <EOL> del self . _buffer [ : ] <EOL> self . send ( msg ) <EOL> def putrequest ( self , method , url , skip_host = <NUM_LIT:0> , skip_accept_encoding = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if self . __response and self . __response . isclosed ( ) : <EOL> self . __response = None <EOL> if self . __state == _CS_IDLE : <EOL> self . __state = _CS_REQ_STARTED <EOL> else : <EOL> raise CannotSendRequest ( ) <EOL> self . _method = method <EOL> if not url : <EOL> url = '<STR_LIT:/>' <EOL> str = '<STR_LIT>' % ( method , url , self . _http_vsn_str ) <EOL> self . _output ( str ) <EOL> if self . _http_vsn == <NUM_LIT:11> : <EOL> if not skip_host : <EOL> netloc = '<STR_LIT>' <EOL> if url . startswith ( '<STR_LIT:http>' ) : <EOL> nil , netloc , nil , nil , nil = urlsplit ( url ) <EOL> if netloc : <EOL> try : <EOL> netloc_enc = netloc . encode ( "<STR_LIT:ascii>" ) <EOL> except UnicodeEncodeError : <EOL> netloc_enc = netloc . encode ( "<STR_LIT>" ) <EOL> self . putheader ( '<STR_LIT>' , netloc_enc ) <EOL> else : <EOL> try : <EOL> host_enc = self . host . encode ( "<STR_LIT:ascii>" ) <EOL> except UnicodeEncodeError : <EOL> host_enc = self . host . encode ( "<STR_LIT>" ) <EOL> if self . port == HTTP_PORT : <EOL> self . putheader ( '<STR_LIT>' , host_enc ) <EOL> else : <EOL> self . putheader ( '<STR_LIT>' , "<STR_LIT>" % ( host_enc , self . port ) ) <EOL> if not skip_accept_encoding : <EOL> self . putheader ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> pass <EOL> def putheader ( self , header , value ) : <EOL> """<STR_LIT>""" <EOL> if self . __state != _CS_REQ_STARTED : <EOL> raise CannotSendHeader ( ) <EOL> str = '<STR_LIT>' % ( header , value ) <EOL> self . _output ( str ) <EOL> def endheaders ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __state == _CS_REQ_STARTED : <EOL> self . __state = _CS_REQ_SENT <EOL> else : <EOL> raise CannotSendHeader ( ) <EOL> self . _send_output ( ) <EOL> def request ( self , method , url , body = None , headers = { } ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _send_request ( method , url , body , headers ) <EOL> except socket . error , v : <EOL> if v [ <NUM_LIT:0> ] != <NUM_LIT:32> or not self . auto_open : <EOL> raise <EOL> self . _send_request ( method , url , body , headers ) <EOL> def _send_request ( self , method , url , body , headers ) : <EOL> header_names = dict . fromkeys ( [ k . lower ( ) for k in headers ] ) <EOL> skips = { } <EOL> if '<STR_LIT:host>' in header_names : <EOL> skips [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> if '<STR_LIT>' in header_names : <EOL> skips [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> self . putrequest ( method , url , ** skips ) <EOL> if body and ( '<STR_LIT>' not in header_names ) : <EOL> self . putheader ( '<STR_LIT>' , str ( len ( body ) ) ) <EOL> for hdr , value in headers . iteritems ( ) : <EOL> self . putheader ( hdr , value ) <EOL> self . endheaders ( ) <EOL> if body : <EOL> self . send ( body ) <EOL> def getresponse ( self ) : <EOL> "<STR_LIT>" <EOL> if self . __response and self . __response . isclosed ( ) : <EOL> self . __response = None <EOL> if self . __state != _CS_REQ_SENT or self . __response : <EOL> raise ResponseNotReady ( ) <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> response = self . response_class ( self . sock , self . debuglevel , <EOL> strict = self . strict , <EOL> method = self . _method ) <EOL> else : <EOL> response = self . response_class ( self . sock , strict = self . strict , <EOL> method = self . _method ) <EOL> response . begin ( ) <EOL> assert response . will_close != _UNKNOWN <EOL> self . __state = _CS_IDLE <EOL> if response . will_close : <EOL> self . close ( ) <EOL> else : <EOL> self . __response = response <EOL> return response <EOL> class SharedSocket : <EOL> def __init__ ( self , sock ) : <EOL> self . sock = sock <EOL> self . _refcnt = <NUM_LIT:0> <EOL> def incref ( self ) : <EOL> self . _refcnt += <NUM_LIT:1> <EOL> def decref ( self ) : <EOL> self . _refcnt -= <NUM_LIT:1> <EOL> assert self . _refcnt >= <NUM_LIT:0> <EOL> if self . _refcnt == <NUM_LIT:0> : <EOL> self . sock . close ( ) <EOL> def __del__ ( self ) : <EOL> self . sock . close ( ) <EOL> class SharedSocketClient : <EOL> def __init__ ( self , shared ) : <EOL> self . _closed = <NUM_LIT:0> <EOL> self . _shared = shared <EOL> self . _shared . incref ( ) <EOL> self . _sock = shared . sock <EOL> def close ( self ) : <EOL> if not self . _closed : <EOL> self . _shared . decref ( ) <EOL> self . _closed = <NUM_LIT:1> <EOL> self . _shared = None <EOL> class SSLFile ( SharedSocketClient ) : <EOL> """<STR_LIT>""" <EOL> BUFSIZE = <NUM_LIT> <EOL> def __init__ ( self , sock , ssl , bufsize = None ) : <EOL> SharedSocketClient . __init__ ( self , sock ) <EOL> self . _ssl = ssl <EOL> self . _buf = '<STR_LIT>' <EOL> self . _bufsize = bufsize or self . __class__ . BUFSIZE <EOL> def _read ( self ) : <EOL> buf = '<STR_LIT>' <EOL> while True : <EOL> try : <EOL> buf = self . _ssl . read ( self . _bufsize ) <EOL> except socket . sslerror , err : <EOL> if ( err [ <NUM_LIT:0> ] == socket . SSL_ERROR_WANT_READ <EOL> or err [ <NUM_LIT:0> ] == socket . SSL_ERROR_WANT_WRITE ) : <EOL> continue <EOL> if ( err [ <NUM_LIT:0> ] == socket . SSL_ERROR_ZERO_RETURN <EOL> or err [ <NUM_LIT:0> ] == socket . SSL_ERROR_EOF ) : <EOL> break <EOL> raise <EOL> except socket . error , err : <EOL> if err [ <NUM_LIT:0> ] == errno . EINTR : <EOL> continue <EOL> if err [ <NUM_LIT:0> ] == errno . EBADF : <EOL> break <EOL> raise <EOL> else : <EOL> break <EOL> return buf <EOL> def read ( self , size = None ) : <EOL> L = [ self . _buf ] <EOL> avail = len ( self . _buf ) <EOL> while size is None or avail < size : <EOL> s = self . _read ( ) <EOL> if s == '<STR_LIT>' : <EOL> break <EOL> L . append ( s ) <EOL> avail += len ( s ) <EOL> all = "<STR_LIT>" . join ( L ) <EOL> if size is None : <EOL> self . _buf = '<STR_LIT>' <EOL> return all <EOL> else : <EOL> self . _buf = all [ size : ] <EOL> return all [ : size ] <EOL> def readline ( self ) : <EOL> L = [ self . _buf ] <EOL> self . _buf = '<STR_LIT>' <EOL> while <NUM_LIT:1> : <EOL> i = L [ - <NUM_LIT:1> ] . find ( "<STR_LIT:\n>" ) <EOL> if i >= <NUM_LIT:0> : <EOL> break <EOL> s = self . _read ( ) <EOL> if s == '<STR_LIT>' : <EOL> break <EOL> L . append ( s ) <EOL> if i == - <NUM_LIT:1> : <EOL> return "<STR_LIT>" . join ( L ) <EOL> else : <EOL> all = "<STR_LIT>" . join ( L ) <EOL> i = all . find ( "<STR_LIT:\n>" ) + <NUM_LIT:1> <EOL> line = all [ : i ] <EOL> self . _buf = all [ i : ] <EOL> return line <EOL> def readlines ( self , sizehint = <NUM_LIT:0> ) : <EOL> total = <NUM_LIT:0> <EOL> list = [ ] <EOL> while True : <EOL> line = self . readline ( ) <EOL> if not line : <EOL> break <EOL> list . append ( line ) <EOL> total += len ( line ) <EOL> if sizehint and total >= sizehint : <EOL> break <EOL> return list <EOL> def fileno ( self ) : <EOL> return self . _sock . fileno ( ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> line = self . readline ( ) <EOL> if not line : <EOL> raise StopIteration <EOL> return line <EOL> class FakeSocket ( SharedSocketClient ) : <EOL> class _closedsocket : <EOL> def __getattr__ ( self , name ) : <EOL> raise error ( <NUM_LIT:9> , '<STR_LIT>' ) <EOL> def __init__ ( self , sock , ssl ) : <EOL> sock = SharedSocket ( sock ) <EOL> SharedSocketClient . __init__ ( self , sock ) <EOL> self . _ssl = ssl <EOL> def close ( self ) : <EOL> SharedSocketClient . close ( self ) <EOL> self . _sock = self . __class__ . _closedsocket ( ) <EOL> def makefile ( self , mode , bufsize = None ) : <EOL> if mode != '<STR_LIT:r>' and mode != '<STR_LIT:rb>' : <EOL> raise UnimplementedFileMode ( ) <EOL> return SSLFile ( self . _shared , self . _ssl , bufsize ) <EOL> def send ( self , stuff , flags = <NUM_LIT:0> ) : <EOL> return self . _ssl . write ( stuff ) <EOL> sendall = send <EOL> def recv ( self , len = <NUM_LIT> , flags = <NUM_LIT:0> ) : <EOL> return self . _ssl . read ( len ) <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . _sock , attr ) <EOL> class HTTPSConnection ( HTTPConnection ) : <EOL> "<STR_LIT>" <EOL> default_port = HTTPS_PORT <EOL> def __init__ ( self , host , port = None , key_file = None , cert_file = None , <EOL> strict = None ) : <EOL> HTTPConnection . __init__ ( self , host , port , strict ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> def connect ( self ) : <EOL> "<STR_LIT>" <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> sock . connect ( ( self . host , self . port ) ) <EOL> ssl = socket . ssl ( sock , self . key_file , self . cert_file ) <EOL> self . sock = FakeSocket ( sock , ssl ) <EOL> class HTTP : <EOL> "<STR_LIT>" <EOL> _http_vsn = <NUM_LIT:10> <EOL> _http_vsn_str = '<STR_LIT>' <EOL> debuglevel = <NUM_LIT:0> <EOL> _connection_class = HTTPConnection <EOL> def __init__ ( self , host = '<STR_LIT>' , port = None , strict = None ) : <EOL> "<STR_LIT>" <EOL> if port == <NUM_LIT:0> : <EOL> port = None <EOL> self . _setup ( self . _connection_class ( host , port , strict ) ) <EOL> def _setup ( self , conn ) : <EOL> self . _conn = conn <EOL> self . send = conn . send <EOL> self . putrequest = conn . putrequest <EOL> self . endheaders = conn . endheaders <EOL> self . set_debuglevel = conn . set_debuglevel <EOL> conn . _http_vsn = self . _http_vsn <EOL> conn . _http_vsn_str = self . _http_vsn_str <EOL> self . file = None <EOL> def connect ( self , host = None , port = None ) : <EOL> "<STR_LIT>" <EOL> if host is not None : <EOL> self . _conn . _set_hostport ( host , port ) <EOL> self . _conn . connect ( ) <EOL> def getfile ( self ) : <EOL> "<STR_LIT>" <EOL> return self . file <EOL> def putheader ( self , header , * values ) : <EOL> "<STR_LIT>" <EOL> self . _conn . putheader ( header , '<STR_LIT>' . join ( values ) ) <EOL> def getreply ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> response = self . _conn . getresponse ( ) <EOL> except BadStatusLine , e : <EOL> self . file = self . _conn . sock . makefile ( '<STR_LIT:rb>' , <NUM_LIT:0> ) <EOL> self . close ( ) <EOL> self . headers = None <EOL> return - <NUM_LIT:1> , e . line , None <EOL> self . headers = response . msg <EOL> self . file = response . fp <EOL> return response . status , response . reason , response . msg <EOL> def close ( self ) : <EOL> self . _conn . close ( ) <EOL> self . file = None <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> class HTTPS ( HTTP ) : <EOL> """<STR_LIT>""" <EOL> _connection_class = HTTPSConnection <EOL> def __init__ ( self , host = '<STR_LIT>' , port = None , key_file = None , cert_file = None , <EOL> strict = None ) : <EOL> if port == <NUM_LIT:0> : <EOL> port = None <EOL> self . _setup ( self . _connection_class ( host , port , key_file , <EOL> cert_file , strict ) ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> class HTTPException ( Exception ) : <EOL> pass <EOL> class NotConnected ( HTTPException ) : <EOL> pass <EOL> class InvalidURL ( HTTPException ) : <EOL> pass <EOL> class UnknownProtocol ( HTTPException ) : <EOL> def __init__ ( self , version ) : <EOL> self . args = version , <EOL> self . version = version <EOL> class UnknownTransferEncoding ( HTTPException ) : <EOL> pass <EOL> class UnimplementedFileMode ( HTTPException ) : <EOL> pass <EOL> class IncompleteRead ( HTTPException ) : <EOL> def __init__ ( self , partial ) : <EOL> self . args = partial , <EOL> self . partial = partial <EOL> class ImproperConnectionState ( HTTPException ) : <EOL> pass <EOL> class CannotSendRequest ( ImproperConnectionState ) : <EOL> pass <EOL> class CannotSendHeader ( ImproperConnectionState ) : <EOL> pass <EOL> class ResponseNotReady ( ImproperConnectionState ) : <EOL> pass <EOL> class BadStatusLine ( HTTPException ) : <EOL> def __init__ ( self , line ) : <EOL> self . args = line , <EOL> self . line = line <EOL> error = HTTPException <EOL> class LineAndFileWrapper : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , line , file ) : <EOL> self . _line = line <EOL> self . _file = file <EOL> self . _line_consumed = <NUM_LIT:0> <EOL> self . _line_offset = <NUM_LIT:0> <EOL> self . _line_left = len ( line ) <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . _file , attr ) <EOL> def _done ( self ) : <EOL> self . _line_consumed = <NUM_LIT:1> <EOL> self . read = self . _file . read <EOL> self . readline = self . _file . readline <EOL> self . readlines = self . _file . readlines <EOL> def read ( self , amt = None ) : <EOL> if self . _line_consumed : <EOL> return self . _file . read ( amt ) <EOL> assert self . _line_left <EOL> if amt is None or amt > self . _line_left : <EOL> s = self . _line [ self . _line_offset : ] <EOL> self . _done ( ) <EOL> if amt is None : <EOL> return s + self . _file . read ( ) <EOL> else : <EOL> return s + self . _file . read ( amt - len ( s ) ) <EOL> else : <EOL> assert amt <= self . _line_left <EOL> i = self . _line_offset <EOL> j = i + amt <EOL> s = self . _line [ i : j ] <EOL> self . _line_offset = j <EOL> self . _line_left -= amt <EOL> if self . _line_left == <NUM_LIT:0> : <EOL> self . _done ( ) <EOL> return s <EOL> def readline ( self ) : <EOL> if self . _line_consumed : <EOL> return self . _file . readline ( ) <EOL> assert self . _line_left <EOL> s = self . _line [ self . _line_offset : ] <EOL> self . _done ( ) <EOL> return s <EOL> def readlines ( self , size = None ) : <EOL> if self . _line_consumed : <EOL> return self . _file . readlines ( size ) <EOL> assert self . _line_left <EOL> L = [ self . _line [ self . _line_offset : ] ] <EOL> self . _done ( ) <EOL> if size is None : <EOL> return L + self . _file . readlines ( ) <EOL> else : <EOL> return L + self . _file . readlines ( size ) <EOL> def test ( ) : <EOL> """<STR_LIT>""" <EOL> import sys <EOL> import getopt <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT:d>' ) <EOL> dl = <NUM_LIT:0> <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : dl = dl + <NUM_LIT:1> <EOL> host = '<STR_LIT>' <EOL> selector = '<STR_LIT:/>' <EOL> if args [ <NUM_LIT:0> : ] : host = args [ <NUM_LIT:0> ] <EOL> if args [ <NUM_LIT:1> : ] : selector = args [ <NUM_LIT:1> ] <EOL> h = HTTP ( ) <EOL> h . set_debuglevel ( dl ) <EOL> h . connect ( host ) <EOL> h . putrequest ( '<STR_LIT:GET>' , selector ) <EOL> h . endheaders ( ) <EOL> status , reason , headers = h . getreply ( ) <EOL> print '<STR_LIT>' , status <EOL> print '<STR_LIT>' , reason <EOL> print "<STR_LIT>" , len ( h . getfile ( ) . read ( ) ) <EOL> print <EOL> if headers : <EOL> for header in headers . headers : print header . strip ( ) <EOL> print <EOL> class HTTP11 ( HTTP ) : <EOL> _http_vsn = <NUM_LIT:11> <EOL> _http_vsn_str = '<STR_LIT>' <EOL> h = HTTP11 ( '<STR_LIT>' ) <EOL> h . putrequest ( '<STR_LIT:GET>' , '<STR_LIT>' ) <EOL> h . endheaders ( ) <EOL> h . getreply ( ) <EOL> h . close ( ) <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> for host , selector in ( ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) : <EOL> print "<STR_LIT>" % ( host , selector ) <EOL> hs = HTTPS ( ) <EOL> hs . set_debuglevel ( dl ) <EOL> hs . connect ( host ) <EOL> hs . putrequest ( '<STR_LIT:GET>' , selector ) <EOL> hs . endheaders ( ) <EOL> status , reason , headers = hs . getreply ( ) <EOL> print '<STR_LIT>' , status <EOL> print '<STR_LIT>' , reason <EOL> print "<STR_LIT>" , len ( hs . getfile ( ) . read ( ) ) <EOL> print <EOL> if headers : <EOL> for header in headers . headers : print header . strip ( ) <EOL> print <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( ) </s>
