<s> r"""<STR_LIT>""" <EOL> from array import array <EOL> import os <EOL> import re <EOL> import socket <EOL> from sys import py3kwarning <EOL> from urlparse import urlsplit <EOL> import warnings <EOL> with warnings . catch_warnings ( ) : <EOL> if py3kwarning : <EOL> warnings . filterwarnings ( "<STR_LIT:ignore>" , "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> import mimetools <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> __all__ = [ "<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> _MAXLINE = <NUM_LIT> <EOL> _MAXHEADERS = <NUM_LIT:100> <EOL> _is_legal_header_name = re . compile ( r'<STR_LIT>' ) . match <EOL> _is_illegal_header_value = re . compile ( r'<STR_LIT>' ) . search <EOL> _METHODS_EXPECTING_BODY = { '<STR_LIT>' , '<STR_LIT:POST>' , '<STR_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 len ( hlist ) > _MAXHEADERS : <EOL> raise HTTPException ( "<STR_LIT>" % _MAXHEADERS ) <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 ( _MAXLINE + <NUM_LIT:1> ) <EOL> if len ( line ) > _MAXLINE : <EOL> raise LineTooLong ( "<STR_LIT>" ) <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> elif headerseen is not None : <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 , buffering = False ) : <EOL> if buffering : <EOL> self . fp = sock . makefile ( '<STR_LIT:rb>' ) <EOL> else : <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 ( _MAXLINE + <NUM_LIT:1> ) <EOL> if len ( line ) > _MAXLINE : <EOL> raise LineTooLong ( "<STR_LIT>" ) <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 ( _MAXLINE + <NUM_LIT:1> ) <EOL> if len ( skip ) > _MAXLINE : <EOL> raise LineTooLong ( "<STR_LIT>" ) <EOL> skip = skip . 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> if self . length < <NUM_LIT:0> : <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> fp = self . fp <EOL> if fp : <EOL> self . fp = None <EOL> fp . close ( ) <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 . _method == '<STR_LIT>' : <EOL> self . close ( ) <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> try : <EOL> s = self . _safe_read ( self . length ) <EOL> except IncompleteRead : <EOL> self . close ( ) <EOL> raise <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 not s and amt : <EOL> self . close ( ) <EOL> if self . length is not None : <EOL> self . length -= len ( s ) <EOL> if not self . length : <EOL> self . close ( ) <EOL> return s <EOL> def _read_chunked ( self , amt ) : <EOL> assert self . chunked != _UNKNOWN <EOL> chunk_left = self . chunk_left <EOL> value = [ ] <EOL> while True : <EOL> if chunk_left is None : <EOL> line = self . fp . readline ( _MAXLINE + <NUM_LIT:1> ) <EOL> if len ( line ) > _MAXLINE : <EOL> raise LineTooLong ( "<STR_LIT>" ) <EOL> i = line . find ( '<STR_LIT:;>' ) <EOL> if i >= <NUM_LIT:0> : <EOL> line = line [ : i ] <EOL> try : <EOL> chunk_left = int ( line , <NUM_LIT:16> ) <EOL> except ValueError : <EOL> self . close ( ) <EOL> raise IncompleteRead ( '<STR_LIT>' . join ( value ) ) <EOL> if chunk_left == <NUM_LIT:0> : <EOL> break <EOL> if amt is None : <EOL> value . append ( self . _safe_read ( chunk_left ) ) <EOL> elif amt < chunk_left : <EOL> value . append ( self . _safe_read ( amt ) ) <EOL> self . chunk_left = chunk_left - amt <EOL> return '<STR_LIT>' . join ( value ) <EOL> elif amt == chunk_left : <EOL> value . append ( self . _safe_read ( amt ) ) <EOL> self . _safe_read ( <NUM_LIT:2> ) <EOL> self . chunk_left = None <EOL> return '<STR_LIT>' . join ( value ) <EOL> else : <EOL> value . append ( 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 ( _MAXLINE + <NUM_LIT:1> ) <EOL> if len ( line ) > _MAXLINE : <EOL> raise LineTooLong ( "<STR_LIT>" ) <EOL> if not line : <EOL> break <EOL> if line == '<STR_LIT:\r\n>' : <EOL> break <EOL> self . close ( ) <EOL> return '<STR_LIT>' . join ( 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 ( '<STR_LIT>' . join ( s ) , amt ) <EOL> s . append ( chunk ) <EOL> amt -= len ( chunk ) <EOL> return '<STR_LIT>' . join ( s ) <EOL> def fileno ( self ) : <EOL> return self . fp . fileno ( ) <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> timeout = socket . _GLOBAL_DEFAULT_TIMEOUT , source_address = None ) : <EOL> self . timeout = timeout <EOL> self . source_address = source_address <EOL> self . sock = None <EOL> self . _buffer = [ ] <EOL> self . __response = None <EOL> self . __state = _CS_IDLE <EOL> self . _method = None <EOL> self . _tunnel_host = None <EOL> self . _tunnel_port = None <EOL> self . _tunnel_headers = { } <EOL> if strict is not None : <EOL> self . strict = strict <EOL> ( self . host , self . port ) = self . _get_hostport ( host , port ) <EOL> self . _create_connection = socket . create_connection <EOL> def set_tunnel ( self , host , port = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if self . sock : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . _tunnel_host = host <EOL> self . _tunnel_port = port <EOL> if headers : <EOL> self . _tunnel_headers = headers <EOL> else : <EOL> self . _tunnel_headers . clear ( ) <EOL> def _get_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> if host [ i + <NUM_LIT:1> : ] == "<STR_LIT>" : <EOL> port = self . default_port <EOL> else : <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> return ( host , port ) <EOL> def set_debuglevel ( self , level ) : <EOL> self . debuglevel = level <EOL> def _tunnel ( self ) : <EOL> ( host , port ) = self . _get_hostport ( self . _tunnel_host , self . _tunnel_port ) <EOL> self . send ( "<STR_LIT>" % ( host , port ) ) <EOL> for header , value in self . _tunnel_headers . iteritems ( ) : <EOL> self . send ( "<STR_LIT>" % ( header , value ) ) <EOL> self . send ( "<STR_LIT:\r\n>" ) <EOL> response = self . response_class ( self . sock , strict = self . strict , <EOL> method = self . _method ) <EOL> ( version , code , message ) = response . _read_status ( ) <EOL> if code != <NUM_LIT:200> : <EOL> self . close ( ) <EOL> raise socket . error ( "<STR_LIT>" % ( code , <EOL> message . strip ( ) ) ) <EOL> while True : <EOL> line = response . fp . readline ( _MAXLINE + <NUM_LIT:1> ) <EOL> if len ( line ) > _MAXLINE : <EOL> raise LineTooLong ( "<STR_LIT>" ) <EOL> if not line : <EOL> break <EOL> if line == '<STR_LIT:\r\n>' : <EOL> break <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . sock = self . _create_connection ( ( self . host , self . port ) , <EOL> self . timeout , self . source_address ) <EOL> if self . _tunnel_host : <EOL> self . _tunnel ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __state = _CS_IDLE <EOL> try : <EOL> sock = self . sock <EOL> if sock : <EOL> self . sock = None <EOL> sock . close ( ) <EOL> finally : <EOL> response = self . __response <EOL> if response : <EOL> self . __response = None <EOL> response . close ( ) <EOL> def send ( self , data ) : <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 ( data ) <EOL> blocksize = <NUM_LIT> <EOL> if hasattr ( data , '<STR_LIT>' ) and not isinstance ( data , array ) : <EOL> if self . debuglevel > <NUM_LIT:0> : print "<STR_LIT>" <EOL> datablock = data . read ( blocksize ) <EOL> while datablock : <EOL> self . sock . sendall ( datablock ) <EOL> datablock = data . read ( blocksize ) <EOL> else : <EOL> self . sock . sendall ( data ) <EOL> def _output ( self , s ) : <EOL> """<STR_LIT>""" <EOL> self . _buffer . append ( s ) <EOL> def _send_output ( self , message_body = None ) : <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> if isinstance ( message_body , str ) : <EOL> msg += message_body <EOL> message_body = None <EOL> self . send ( msg ) <EOL> if message_body is not None : <EOL> self . send ( message_body ) <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> hdr = '<STR_LIT>' % ( method , url , self . _http_vsn_str ) <EOL> self . _output ( hdr ) <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> if self . _tunnel_host : <EOL> host = self . _tunnel_host <EOL> port = self . _tunnel_port <EOL> else : <EOL> host = self . host <EOL> port = self . port <EOL> try : <EOL> host_enc = host . encode ( "<STR_LIT:ascii>" ) <EOL> except UnicodeEncodeError : <EOL> host_enc = host . encode ( "<STR_LIT>" ) <EOL> if host_enc . find ( '<STR_LIT::>' ) >= <NUM_LIT:0> : <EOL> host_enc = "<STR_LIT:[>" + host_enc + "<STR_LIT:]>" <EOL> if port == self . default_port : <EOL> self . putheader ( '<STR_LIT>' , host_enc ) <EOL> else : <EOL> self . putheader ( '<STR_LIT>' , "<STR_LIT>" % ( host_enc , port ) ) <EOL> if not skip_accept_encoding : <EOL> self . putheader ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> pass <EOL> def putheader ( self , header , * values ) : <EOL> """<STR_LIT>""" <EOL> if self . __state != _CS_REQ_STARTED : <EOL> raise CannotSendHeader ( ) <EOL> header = '<STR_LIT:%s>' % header <EOL> if not _is_legal_header_name ( header ) : <EOL> raise ValueError ( '<STR_LIT>' % ( header , ) ) <EOL> values = [ str ( v ) for v in values ] <EOL> for one_value in values : <EOL> if _is_illegal_header_value ( one_value ) : <EOL> raise ValueError ( '<STR_LIT>' % ( one_value , ) ) <EOL> hdr = '<STR_LIT>' % ( header , '<STR_LIT>' . join ( values ) ) <EOL> self . _output ( hdr ) <EOL> def endheaders ( self , message_body = None ) : <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 ( message_body ) <EOL> def request ( self , method , url , body = None , headers = { } ) : <EOL> """<STR_LIT>""" <EOL> self . _send_request ( method , url , body , headers ) <EOL> def _set_content_length ( self , body , method ) : <EOL> thelen = None <EOL> if body is None and method . upper ( ) in _METHODS_EXPECTING_BODY : <EOL> thelen = '<STR_LIT:0>' <EOL> elif body is not None : <EOL> try : <EOL> thelen = str ( len ( body ) ) <EOL> except ( TypeError , AttributeError ) : <EOL> try : <EOL> thelen = str ( os . fstat ( body . fileno ( ) ) . st_size ) <EOL> except ( AttributeError , OSError ) : <EOL> if self . debuglevel > <NUM_LIT:0> : print "<STR_LIT>" <EOL> if thelen is not None : <EOL> self . putheader ( '<STR_LIT>' , thelen ) <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 '<STR_LIT>' not in header_names : <EOL> self . _set_content_length ( body , method ) <EOL> for hdr , value in headers . iteritems ( ) : <EOL> self . putheader ( hdr , value ) <EOL> self . endheaders ( body ) <EOL> def getresponse ( self , buffering = False ) : <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> args = ( self . sock , ) <EOL> kwds = { "<STR_LIT:strict>" : self . strict , "<STR_LIT>" : self . _method } <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> args += ( self . debuglevel , ) <EOL> if buffering : <EOL> kwds [ "<STR_LIT>" ] = True ; <EOL> response = self . response_class ( * args , ** kwds ) <EOL> try : <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> except : <EOL> response . close ( ) <EOL> raise <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 . putheader = conn . putheader <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 . host , self . _conn . port ) = self . _conn . _get_hostport ( host , port ) <EOL> self . _conn . connect ( ) <EOL> def getfile ( self ) : <EOL> "<STR_LIT>" <EOL> return self . file <EOL> def getreply ( self , buffering = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if not buffering : <EOL> response = self . _conn . getresponse ( ) <EOL> else : <EOL> response = self . _conn . getresponse ( buffering ) <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> try : <EOL> import ssl <EOL> except ImportError : <EOL> pass <EOL> else : <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 , timeout = socket . _GLOBAL_DEFAULT_TIMEOUT , <EOL> source_address = None , context = None ) : <EOL> HTTPConnection . __init__ ( self , host , port , strict , timeout , <EOL> source_address ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> if context is None : <EOL> context = ssl . _create_default_https_context ( ) <EOL> if key_file or cert_file : <EOL> context . load_cert_chain ( cert_file , key_file ) <EOL> self . _context = context <EOL> def connect ( self ) : <EOL> "<STR_LIT>" <EOL> HTTPConnection . connect ( self ) <EOL> if self . _tunnel_host : <EOL> server_hostname = self . _tunnel_host <EOL> else : <EOL> server_hostname = self . host <EOL> self . sock = self . _context . wrap_socket ( self . sock , <EOL> server_hostname = server_hostname ) <EOL> __all__ . append ( "<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 , context = 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> context = context ) ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> def FakeSocket ( sock , sslobj ) : <EOL> warnings . warn ( "<STR_LIT>" + <EOL> "<STR_LIT>" , <EOL> DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> return sslobj <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 , expected = None ) : <EOL> self . args = partial , <EOL> self . partial = partial <EOL> self . expected = expected <EOL> def __repr__ ( self ) : <EOL> if self . expected is not None : <EOL> e = '<STR_LIT>' % self . expected <EOL> else : <EOL> e = '<STR_LIT>' <EOL> return '<STR_LIT>' % ( len ( self . partial ) , e ) <EOL> def __str__ ( self ) : <EOL> return repr ( self ) <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> if not line : <EOL> line = repr ( line ) <EOL> self . args = line , <EOL> self . line = line <EOL> class LineTooLong ( HTTPException ) : <EOL> def __init__ ( self , line_type ) : <EOL> HTTPException . __init__ ( self , "<STR_LIT>" <EOL> % ( _MAXLINE , line_type ) ) <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 ) </s>
