<s> """<STR_LIT>""" <EOL> import urllib <EOL> import zlib <EOL> from Cookie import SimpleCookie <EOL> from urlparse import urlparse , urlunparse , urljoin , urlsplit <EOL> from datetime import datetime <EOL> from . auth import dispatch as auth_dispatch <EOL> from . hooks import dispatch_hook <EOL> from . structures import CaseInsensitiveDict <EOL> from . status_codes import codes <EOL> from . packages . urllib3 . exceptions import MaxRetryError <EOL> from . packages . urllib3 . exceptions import SSLError as _SSLError <EOL> from . packages . urllib3 . exceptions import HTTPError as _HTTPError <EOL> from . packages . urllib3 import connectionpool , poolmanager <EOL> from . packages . urllib3 . filepost import encode_multipart_formdata <EOL> from . exceptions import ( <EOL> Timeout , URLRequired , TooManyRedirects , HTTPError , ConnectionError ) <EOL> from . utils import ( <EOL> get_unicode_from_response , stream_decode_response_unicode , <EOL> decode_gzip , stream_decode_gzip , guess_filename ) <EOL> REDIRECT_STATI = ( codes . moved , codes . found , codes . other , codes . temporary_moved ) <EOL> class Request ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> url = None , <EOL> headers = dict ( ) , <EOL> files = None , <EOL> method = None , <EOL> data = dict ( ) , <EOL> params = dict ( ) , <EOL> auth = None , <EOL> cookies = None , <EOL> timeout = None , <EOL> redirect = False , <EOL> allow_redirects = False , <EOL> proxies = None , <EOL> hooks = None , <EOL> config = None , <EOL> _poolmanager = None ) : <EOL> self . timeout = timeout <EOL> self . url = url <EOL> self . headers = dict ( headers or [ ] ) <EOL> self . files = files <EOL> self . method = method <EOL> self . data = None <EOL> self . params = None <EOL> self . params = dict ( params or [ ] ) <EOL> self . redirect = redirect <EOL> self . allow_redirects = allow_redirects <EOL> self . proxies = dict ( proxies or [ ] ) <EOL> self . data , self . _enc_data = self . _encode_params ( data ) <EOL> self . params , self . _enc_params = self . _encode_params ( params ) <EOL> self . response = Response ( ) <EOL> self . _auth = auth <EOL> self . auth = auth_dispatch ( auth ) <EOL> self . cookies = dict ( cookies or [ ] ) <EOL> self . config = dict ( config or [ ] ) <EOL> self . sent = False <EOL> self . hooks = hooks <EOL> self . session = None <EOL> if headers : <EOL> headers = CaseInsensitiveDict ( self . headers ) <EOL> else : <EOL> headers = CaseInsensitiveDict ( ) <EOL> for ( k , v ) in self . config . get ( '<STR_LIT>' , { } ) . items ( ) : <EOL> if k not in headers : <EOL> headers [ k ] = v <EOL> self . headers = headers <EOL> self . _poolmanager = _poolmanager <EOL> r = dispatch_hook ( '<STR_LIT>' , hooks , self ) <EOL> self . __dict__ . update ( r . __dict__ ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . method ) <EOL> def _build_response ( self , resp , is_error = False ) : <EOL> """<STR_LIT>""" <EOL> def build ( resp ) : <EOL> response = Response ( ) <EOL> response . config = self . config <EOL> if resp : <EOL> response . status_code = getattr ( resp , '<STR_LIT:status>' , None ) <EOL> response . headers = CaseInsensitiveDict ( getattr ( resp , '<STR_LIT>' , None ) ) <EOL> cookies = self . cookies or dict ( ) <EOL> if '<STR_LIT>' in response . headers : <EOL> cookie_header = response . headers [ '<STR_LIT>' ] <EOL> c = SimpleCookie ( ) <EOL> c . load ( cookie_header ) <EOL> for k , v in c . items ( ) : <EOL> cookies . update ( { k : v . value } ) <EOL> response . cookies = cookies <EOL> response . raw = resp <EOL> if is_error : <EOL> response . error = resp <EOL> response . url = self . full_url <EOL> return response <EOL> history = [ ] <EOL> r = build ( resp ) <EOL> cookies = self . cookies <EOL> self . cookies . update ( r . cookies ) <EOL> if r . status_code in REDIRECT_STATI and not self . redirect : <EOL> while ( <EOL> ( '<STR_LIT:location>' in r . headers ) and <EOL> ( ( r . status_code is codes . see_other ) or ( self . allow_redirects ) ) <EOL> ) : <EOL> if not len ( history ) < self . config . get ( '<STR_LIT>' ) : <EOL> raise TooManyRedirects ( ) <EOL> history . append ( r ) <EOL> url = r . headers [ '<STR_LIT:location>' ] <EOL> if url . startswith ( '<STR_LIT>' ) : <EOL> parsed_rurl = urlparse ( r . url ) <EOL> url = '<STR_LIT>' % ( parsed_rurl . scheme , url ) <EOL> if not urlparse ( url ) . netloc : <EOL> url = urljoin ( r . url , urllib . quote ( urllib . unquote ( url ) ) ) <EOL> if r . status_code is codes . see_other : <EOL> method = '<STR_LIT:GET>' <EOL> else : <EOL> method = self . method <EOL> headers = self . headers <EOL> try : <EOL> del headers [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> request = Request ( <EOL> url = url , <EOL> headers = headers , <EOL> files = self . files , <EOL> method = method , <EOL> params = self . session . params , <EOL> auth = self . _auth , <EOL> cookies = cookies , <EOL> redirect = True , <EOL> config = self . config , <EOL> timeout = self . timeout , <EOL> _poolmanager = self . _poolmanager , <EOL> proxies = self . proxies , <EOL> ) <EOL> request . send ( ) <EOL> cookies . update ( request . response . cookies ) <EOL> r = request . response <EOL> self . cookies . update ( r . cookies ) <EOL> r . history = history <EOL> self . response = r <EOL> self . response . request = self <EOL> self . response . cookies . update ( self . cookies ) <EOL> @ staticmethod <EOL> def _encode_params ( data ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> data = dict ( data ) <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> result = [ ] <EOL> for k , vs in data . items ( ) : <EOL> for v in isinstance ( vs , list ) and vs or [ vs ] : <EOL> result . append ( ( k . encode ( '<STR_LIT:utf-8>' ) if isinstance ( k , unicode ) else k , <EOL> v . encode ( '<STR_LIT:utf-8>' ) if isinstance ( v , unicode ) else v ) ) <EOL> return result , urllib . urlencode ( result , doseq = True ) <EOL> else : <EOL> return data , data <EOL> @ property <EOL> def full_url ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . url : <EOL> raise URLRequired ( ) <EOL> scheme , netloc , path , params , query , fragment = urlparse ( self . url ) <EOL> if not scheme : <EOL> raise ValueError ( ) <EOL> netloc = netloc . encode ( '<STR_LIT>' ) <EOL> if isinstance ( path , unicode ) : <EOL> path = path . encode ( '<STR_LIT:utf-8>' ) <EOL> path = urllib . quote ( urllib . unquote ( path ) ) <EOL> url = str ( urlunparse ( [ scheme , netloc , path , params , query , fragment ] ) ) <EOL> if self . _enc_params : <EOL> if urlparse ( url ) . query : <EOL> return '<STR_LIT>' % ( url , self . _enc_params ) <EOL> else : <EOL> return '<STR_LIT>' % ( url , self . _enc_params ) <EOL> else : <EOL> return url <EOL> @ property <EOL> def path_url ( self ) : <EOL> """<STR_LIT>""" <EOL> url = [ ] <EOL> p = urlsplit ( self . full_url ) <EOL> if p . scheme in self . proxies : <EOL> return self . full_url <EOL> path = p . path <EOL> if not path : <EOL> path = '<STR_LIT:/>' <EOL> url . append ( path ) <EOL> query = p . query <EOL> if query : <EOL> url . append ( '<STR_LIT:?>' ) <EOL> url . append ( query ) <EOL> return '<STR_LIT>' . join ( url ) <EOL> def send ( self , anyway = False , prefetch = False ) : <EOL> """<STR_LIT>""" <EOL> if self . config . get ( '<STR_LIT>' ) : <EOL> self . config . get ( '<STR_LIT>' ) . write ( '<STR_LIT>' % ( <EOL> datetime . now ( ) . isoformat ( ) , self . method , self . url <EOL> ) ) <EOL> url = self . full_url <EOL> body = None <EOL> content_type = None <EOL> if self . files : <EOL> if not isinstance ( self . data , basestring ) : <EOL> try : <EOL> fields = self . data . copy ( ) <EOL> except AttributeError : <EOL> fields = dict ( self . data ) <EOL> for ( k , v ) in self . files . items ( ) : <EOL> fields . update ( { k : ( guess_filename ( k ) or k , v . read ( ) ) } ) <EOL> ( body , content_type ) = encode_multipart_formdata ( fields ) <EOL> else : <EOL> pass <EOL> else : <EOL> if self . data : <EOL> body = self . _enc_data <EOL> if isinstance ( self . data , basestring ) : <EOL> content_type = None <EOL> else : <EOL> content_type = '<STR_LIT>' <EOL> if ( content_type ) and ( not '<STR_LIT>' in self . headers ) : <EOL> self . headers [ '<STR_LIT:Content-Type>' ] = content_type <EOL> if self . auth : <EOL> auth_func , auth_args = self . auth <EOL> r = auth_func ( self , * auth_args ) <EOL> self . __dict__ . update ( r . __dict__ ) <EOL> _p = urlparse ( url ) <EOL> proxy = self . proxies . get ( _p . scheme ) <EOL> if proxy : <EOL> conn = poolmanager . proxy_from_url ( proxy ) <EOL> else : <EOL> if self . config . get ( '<STR_LIT>' ) : <EOL> conn = self . _poolmanager . connection_from_url ( url ) <EOL> else : <EOL> conn = connectionpool . connection_from_url ( url ) <EOL> if not self . sent or anyway : <EOL> if self . cookies : <EOL> if '<STR_LIT>' not in self . headers : <EOL> c = SimpleCookie ( ) <EOL> for ( k , v ) in self . cookies . items ( ) : <EOL> c [ k ] = v <EOL> cookie_header = c . output ( header = '<STR_LIT>' ) . strip ( ) <EOL> self . headers [ '<STR_LIT>' ] = cookie_header <EOL> try : <EOL> r = conn . urlopen ( <EOL> method = self . method , <EOL> url = self . path_url , <EOL> body = body , <EOL> headers = self . headers , <EOL> redirect = False , <EOL> assert_same_host = False , <EOL> preload_content = prefetch , <EOL> decode_content = False , <EOL> retries = self . config . get ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> timeout = self . timeout , <EOL> ) <EOL> except MaxRetryError , e : <EOL> if not self . config . get ( '<STR_LIT>' , False ) : <EOL> raise ConnectionError ( e ) <EOL> else : <EOL> r = None <EOL> except ( _SSLError , _HTTPError ) , e : <EOL> if not self . config . get ( '<STR_LIT>' , False ) : <EOL> raise Timeout ( '<STR_LIT>' ) <EOL> self . _build_response ( r ) <EOL> self . response = dispatch_hook ( '<STR_LIT>' , self . hooks , self . response ) <EOL> r = dispatch_hook ( '<STR_LIT>' , self . hooks , self ) <EOL> self . __dict__ . update ( r . __dict__ ) <EOL> if prefetch : <EOL> self . response . _content_consumed = True <EOL> return self . sent <EOL> class Response ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _content = None <EOL> self . _content_consumed = False <EOL> self . status_code = None <EOL> self . headers = CaseInsensitiveDict ( ) <EOL> self . raw = None <EOL> self . url = None <EOL> self . error = None <EOL> self . history = [ ] <EOL> self . request = None <EOL> self . cookies = { } <EOL> self . config = { } <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . status_code ) <EOL> def __nonzero__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . ok <EOL> @ property <EOL> def ok ( self ) : <EOL> try : <EOL> self . raise_for_status ( ) <EOL> except HTTPError : <EOL> return False <EOL> return True <EOL> def iter_content ( self , chunk_size = <NUM_LIT:10> * <NUM_LIT> , decode_unicode = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _content_consumed : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> def generate ( ) : <EOL> while <NUM_LIT:1> : <EOL> chunk = self . raw . read ( chunk_size ) <EOL> if not chunk : <EOL> break <EOL> yield chunk <EOL> self . _content_consumed = True <EOL> gen = generate ( ) <EOL> if '<STR_LIT>' in self . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> gen = stream_decode_gzip ( gen ) <EOL> if decode_unicode is None : <EOL> decode_unicode = self . config . get ( '<STR_LIT>' ) <EOL> if decode_unicode : <EOL> gen = stream_decode_response_unicode ( gen , self ) <EOL> return gen <EOL> @ property <EOL> def content ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _content is not None : <EOL> return self . _content <EOL> if self . _content_consumed : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> try : <EOL> self . _content = self . raw . read ( ) <EOL> except AttributeError : <EOL> return None <EOL> if '<STR_LIT>' in self . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> try : <EOL> self . _content = decode_gzip ( self . _content ) <EOL> except zlib . error : <EOL> pass <EOL> if self . config . get ( '<STR_LIT>' ) : <EOL> self . _content = get_unicode_from_response ( self ) <EOL> self . _content_consumed = True <EOL> return self . _content <EOL> def raise_for_status ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . error : <EOL> raise self . error <EOL> if ( self . status_code >= <NUM_LIT> ) and ( self . status_code < <NUM_LIT> ) : <EOL> raise HTTPError ( '<STR_LIT>' % self . status_code ) <EOL> elif ( self . status_code >= <NUM_LIT> ) and ( self . status_code < <NUM_LIT> ) : <EOL> raise HTTPError ( '<STR_LIT>' % self . status_code ) <EOL> elif ( self . status_code >= <NUM_LIT> ) and ( self . status_code < <NUM_LIT> ) : <EOL> raise HTTPError ( '<STR_LIT>' % self . status_code ) </s>
