<s> import requests <EOL> import simplejson <EOL> import ssl <EOL> from . exception import APIError <EOL> class Stream : <EOL> base_url = '<STR_LIT>' <EOL> def __init__ ( self , api_key ) : <EOL> self . api_key = api_key <EOL> def _create_stream ( self , name , timeout = None ) : <EOL> if timeout <= <NUM_LIT:0> : <EOL> timeout = None <EOL> try : <EOL> req = requests . get ( self . base_url + name , params = { '<STR_LIT:key>' : self . api_key } , stream = True , timeout = timeout ) <EOL> except Exception as e : <EOL> raise APIError ( '<STR_LIT>' ) <EOL> if req . status_code != <NUM_LIT:200> : <EOL> try : <EOL> data = simplejson . loads ( req . text ) <EOL> raise APIError ( data [ '<STR_LIT:error>' ] ) <EOL> except APIError as e : <EOL> raise <EOL> except Exception as e : <EOL> pass <EOL> raise APIError ( '<STR_LIT>' ) <EOL> return req <EOL> def _iter_stream ( self , stream , raw ) : <EOL> for line in stream . iter_lines ( ) : <EOL> if line : <EOL> if raw : <EOL> yield line <EOL> else : <EOL> yield simplejson . loads ( line ) <EOL> def alert ( self , aid = None , timeout = None , raw = False ) : <EOL> if aid : <EOL> stream = self . _create_stream ( '<STR_LIT>' % aid , timeout = timeout ) <EOL> else : <EOL> stream = self . _create_stream ( '<STR_LIT>' , timeout = timeout ) <EOL> try : <EOL> for line in self . _iter_stream ( stream , raw ) : <EOL> yield line <EOL> except requests . exceptions . ConnectionError as e : <EOL> raise APIError ( '<STR_LIT>' ) <EOL> except ssl . SSLError as e : <EOL> raise APIError ( '<STR_LIT>' ) <EOL> def asn ( self , asn , raw = False , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> stream = self . _create_stream ( '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( asn ) , timeout = timeout ) <EOL> for line in self . _iter_stream ( stream , raw ) : <EOL> yield line <EOL> def banners ( self , raw = False , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> stream = self . _create_stream ( '<STR_LIT>' , timeout = timeout ) <EOL> for line in self . _iter_stream ( stream , raw ) : <EOL> yield line <EOL> def countries ( self , countries , raw = False , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> stream = self . _create_stream ( '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( countries ) , timeout = timeout ) <EOL> for line in self . _iter_stream ( stream , raw ) : <EOL> yield line <EOL> def ports ( self , ports , raw = False , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> stream = self . _create_stream ( '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( [ str ( port ) for port in ports ] ) , timeout = timeout ) <EOL> for line in self . _iter_stream ( stream , raw ) : <EOL> yield line </s>
