<s> import re <EOL> import socket <EOL> import ssl <EOL> import sys <EOL> _using_requests = False <EOL> try : <EOL> import requests <EOL> _using_requests = True <EOL> except ImportError : <EOL> try : <EOL> from urllib . request import Request , build_opener , HTTPErrorProcessor , HTTPSHandler <EOL> from urllib . error import URLError <EOL> from urllib . parse import urlencode <EOL> except ImportError : <EOL> from urllib2 import Request , URLError , build_opener , HTTPErrorProcessor , HTTPSHandler <EOL> from urllib import urlencode <EOL> def _isunicode ( s ) : <EOL> try : <EOL> if isinstance ( s , unicode ) : <EOL> return True <EOL> return False <EOL> except NameError : <EOL> if isinstance ( s , str ) : <EOL> return True <EOL> return False <EOL> class PanHttpError ( Exception ) : <EOL> pass <EOL> class PanHttp : <EOL> def __init__ ( self , <EOL> timeout = None , <EOL> verify_cert = True ) : <EOL> self . timeout = timeout <EOL> self . verify_cert = verify_cert <EOL> if self . timeout is not None : <EOL> try : <EOL> self . timeout = float ( self . timeout ) <EOL> if not self . timeout >= <NUM_LIT:0> : <EOL> raise ValueError <EOL> except ValueError : <EOL> raise PanHttpError ( '<STR_LIT>' % self . timeout ) <EOL> self . using_requests = _using_requests <EOL> if self . using_requests : <EOL> self . _http_request = self . _http_request_requests <EOL> if not self . verify_cert : <EOL> requests . packages . urllib3 . disable_warnings ( ) <EOL> self . requests_version = requests . __version__ <EOL> else : <EOL> self . _http_request = self . _http_request_urllib <EOL> def _init_attributes ( self ) : <EOL> self . code = None <EOL> self . reason = None <EOL> self . headers = None <EOL> self . encoding = None <EOL> self . text = None <EOL> self . content = None <EOL> def http_request ( self , url = None , headers = None , data = None , params = None ) : <EOL> self . _init_attributes ( ) <EOL> self . _http_request ( url , headers , data , params ) <EOL> def raise_for_status ( self ) : <EOL> if self . code is None : <EOL> return None <EOL> if not ( <NUM_LIT:200> <= self . code < <NUM_LIT> ) : <EOL> e = '<STR_LIT>' % self . code <EOL> if self . reason is not None : <EOL> e += '<STR_LIT>' % self . reason <EOL> raise PanHttpError ( e ) <EOL> return None <EOL> def _http_request_urllib ( self , url , headers , data , params ) : <EOL> if _isunicode ( data ) : <EOL> data = data . encode ( ) <EOL> if params is not None : <EOL> url += '<STR_LIT:?>' + urlencode ( params ) <EOL> kwargs = { } <EOL> if url is not None : <EOL> kwargs [ '<STR_LIT:url>' ] = url <EOL> if headers is not None : <EOL> kwargs [ '<STR_LIT>' ] = headers <EOL> if data is not None : <EOL> kwargs [ '<STR_LIT:data>' ] = data <EOL> request = Request ( ** kwargs ) <EOL> kwargs = { <EOL> '<STR_LIT:url>' : request , <EOL> } <EOL> if self . timeout is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . timeout <EOL> if not self . verify_cert and ( sys . version_info . major == <NUM_LIT:2> and <EOL> sys . hexversion >= <NUM_LIT> or <EOL> sys . version_info . major == <NUM_LIT:3> and <EOL> sys . hexversion >= <NUM_LIT> ) : <EOL> context = ssl . _create_unverified_context ( ) <EOL> kwargs [ '<STR_LIT>' ] = context <EOL> try : <EOL> response = self . _urlopen ( ** kwargs ) <EOL> except ( URLError , IOError , ssl . CertificateError ) as e : <EOL> raise PanHttpError ( str ( e ) ) <EOL> self . code = response . getcode ( ) <EOL> if hasattr ( response , '<STR_LIT>' ) : <EOL> self . reason = response . reason <EOL> elif hasattr ( response , '<STR_LIT>' ) : <EOL> self . reason = response . msg <EOL> h = dict ( response . info ( ) . items ( ) ) <EOL> self . headers = dict ( ( k . lower ( ) , v ) for ( k , v ) in h . items ( ) ) <EOL> s = self . headers . get ( '<STR_LIT>' ) <EOL> self . encoding = '<STR_LIT:utf8>' <EOL> if s is not None : <EOL> types = [ x . lower ( ) for x in s . split ( '<STR_LIT:;>' ) ] <EOL> types = [ x . lstrip ( ) for x in types ] <EOL> types = [ x . rstrip ( ) for x in types ] <EOL> types = set ( types ) <EOL> for x in types : <EOL> result = re . search ( r'<STR_LIT>' , x ) <EOL> if result : <EOL> self . encoding = result . group ( <NUM_LIT:1> ) <EOL> self . content = response . read ( ) <EOL> self . text = self . content . decode ( self . encoding ) <EOL> def _http_request_requests ( self , url , headers , data , params ) : <EOL> kwargs = { <EOL> '<STR_LIT>' : self . verify_cert , <EOL> } <EOL> if url is not None : <EOL> kwargs [ '<STR_LIT:url>' ] = url <EOL> if headers is not None : <EOL> kwargs [ '<STR_LIT>' ] = headers <EOL> if data is not None : <EOL> kwargs [ '<STR_LIT:data>' ] = data <EOL> if params is not None : <EOL> kwargs [ '<STR_LIT>' ] = params <EOL> if self . timeout is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . timeout <EOL> try : <EOL> if data is None : <EOL> r = requests . get ( ** kwargs ) <EOL> else : <EOL> r = requests . post ( ** kwargs ) <EOL> except requests . exceptions . RequestException as e : <EOL> raise PanHttpError ( str ( e ) ) <EOL> self . code = r . status_code <EOL> self . reason = r . reason <EOL> self . headers = r . headers <EOL> self . encoding = r . encoding <EOL> self . content = r . content <EOL> self . text = r . text <EOL> @ staticmethod <EOL> def _urlopen ( url , data = None , <EOL> timeout = socket . _GLOBAL_DEFAULT_TIMEOUT , <EOL> cafile = None , capath = None , cadefault = False , <EOL> context = None ) : <EOL> def http_response ( request , response ) : <EOL> return response <EOL> http_error_processor = HTTPErrorProcessor ( ) <EOL> http_error_processor . https_response = http_response <EOL> if context : <EOL> https_handler = HTTPSHandler ( context = context ) <EOL> opener = build_opener ( https_handler , http_error_processor ) <EOL> else : <EOL> opener = build_opener ( http_error_processor ) <EOL> return opener . open ( url , data , timeout ) </s>
