<s> import sys , os , datetime <EOL> import urllib <EOL> import ssl <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT:..>" , "<STR_LIT:..>" ) ) <EOL> import splunklib . binding as binding <EOL> import splunklib . client as client <EOL> try : <EOL> from utils import parse , error <EOL> except ImportError : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> urllib2 = None <EOL> def _spliturl ( url ) : <EOL> scheme , part = url . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> host , path = urllib . splithost ( part ) <EOL> host , port = urllib . splitnport ( host , <NUM_LIT> ) <EOL> return scheme , host , port , path <EOL> def main ( argv ) : <EOL> global urllib2 <EOL> usage = "<STR_LIT>" <EOL> opts = parse ( argv , { } , "<STR_LIT>" ) <EOL> allowed_args = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> if len ( opts . args ) == <NUM_LIT:0> or opts . args [ <NUM_LIT:0> ] not in allowed_args : <EOL> error ( "<STR_LIT>" % allowed_args , <NUM_LIT:2> ) <EOL> is_async = opts . args [ <NUM_LIT:0> ] == "<STR_LIT>" <EOL> if is_async : <EOL> urllib2 = __import__ ( '<STR_LIT>' , globals ( ) , locals ( ) , <EOL> [ '<STR_LIT>' ] , - <NUM_LIT:1> ) . urllib2 <EOL> else : <EOL> urllib2 = __import__ ( "<STR_LIT>" , globals ( ) , locals ( ) , [ ] , - <NUM_LIT:1> ) <EOL> service = client . Service ( handler = request , ** opts . kwargs ) <EOL> service . login ( ) <EOL> oldtime = datetime . datetime . now ( ) <EOL> def do_search ( query ) : <EOL> job = service . jobs . create ( query , exec_mode = "<STR_LIT>" ) <EOL> results = job . results ( ) <EOL> job . cancel ( ) <EOL> return results <EOL> queries = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> if is_async : <EOL> import eventlet <EOL> pool = eventlet . GreenPool ( <NUM_LIT:16> ) <EOL> for results in pool . imap ( do_search , queries ) : <EOL> pass <EOL> else : <EOL> for query in queries : <EOL> do_search ( query ) <EOL> newtime = datetime . datetime . now ( ) <EOL> print "<STR_LIT>" % ( newtime - oldtime ) <EOL> def request ( url , message , ** kwargs ) : <EOL> scheme , host , port , path = _spliturl ( url ) <EOL> body = message . get ( "<STR_LIT:body>" , "<STR_LIT>" ) <EOL> head = { <EOL> "<STR_LIT>" : str ( len ( body ) ) , <EOL> "<STR_LIT>" : host , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> for key , value in message [ "<STR_LIT>" ] : <EOL> head [ key ] = value <EOL> method = message . get ( "<STR_LIT>" , "<STR_LIT:GET>" ) <EOL> if sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT:9> ) : <EOL> unverified_ssl_handler = urllib2 . HTTPSHandler ( context = ssl . _create_unverified_context ( ) ) <EOL> opener = urllib2 . build_opener ( unverified_ssl_handler ) <EOL> else : <EOL> opener = urllib2 . build_opener ( ) <EOL> request = urllib2 . Request ( url , body , head ) <EOL> request . get_method = lambda : method <EOL> response = None <EOL> try : <EOL> response = opener . open ( request ) <EOL> except Exception as e : <EOL> response = e <EOL> return { <EOL> '<STR_LIT:status>' : response . code , <EOL> '<STR_LIT>' : response . msg , <EOL> '<STR_LIT>' : response . info ( ) . dict , <EOL> '<STR_LIT:body>' : binding . ResponseReader ( response ) <EOL> } <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( sys . argv [ <NUM_LIT:1> : ] ) </s>
