<s> """<STR_LIT>""" <EOL> import sys <EOL> import socket <EOL> import httplib <EOL> import urllib <EOL> import base64 <EOL> import simplejson as json <EOL> import string <EOL> class RestClient : <EOL> def __init__ ( self , server , port , opts = { } ) : <EOL> self . server = server <EOL> self . port = port <EOL> self . debug = opts . get ( '<STR_LIT>' , False ) <EOL> self . uri = '<STR_LIT>' <EOL> self . method = '<STR_LIT:GET>' <EOL> self . params = { } <EOL> self . user = '<STR_LIT>' <EOL> self . password = '<STR_LIT>' <EOL> self . clientConnect ( server , int ( port ) ) <EOL> def clientConnect ( self , server , port ) : <EOL> error_connect = "<STR_LIT>" % self . server <EOL> try : <EOL> self . conn = httplib . HTTPConnection ( server , port ) <EOL> except httplib . NotConnected : <EOL> print error_connect <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> except httplib . HTTPException : <EOL> print error_connect <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> except socket . error : <EOL> print error_connect <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> except socket . gaierror : <EOL> print error_connect <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def setParam ( self , param , value ) : <EOL> self . params [ param ] = value <EOL> def handleResponse ( self , <EOL> method , <EOL> response , <EOL> opts = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) : <EOL> """<STR_LIT>""" <EOL> if response . status in [ <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] : <EOL> if method == '<STR_LIT:GET>' : <EOL> return response . read ( ) <EOL> return "<STR_LIT>" % opts [ '<STR_LIT>' ] <EOL> if response . status == <NUM_LIT> : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> print '<STR_LIT>' % ( opts [ '<STR_LIT>' ] , <EOL> response . status , response . reason ) <EOL> output_json = json . loads ( response . read ( ) ) <EOL> print output_json <EOL> if "<STR_LIT>" in output_json : <EOL> for error_code , error_message in output_json [ "<STR_LIT>" ] . iteritems ( ) : <EOL> print "<STR_LIT>" % error_message <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def bootStrap ( self , headers ) : <EOL> """<STR_LIT>""" <EOL> self . conn . request ( '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , headers ) <EOL> response = self . conn . getresponse ( ) <EOL> opts = { '<STR_LIT>' : '<STR_LIT>' } <EOL> return self . handleResponse ( '<STR_LIT:GET>' , response , opts ) <EOL> def sendCmd ( self , method , uri , <EOL> user = '<STR_LIT>' , password = '<STR_LIT>' , opts = { } ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> headers = { } <EOL> encoded_params = '<STR_LIT>' <EOL> if user and password : <EOL> self . user = user <EOL> self . password = password <EOL> auth = ( '<STR_LIT>' + <EOL> string . strip ( base64 . encodestring ( user + '<STR_LIT::>' + password ) ) ) <EOL> headers [ '<STR_LIT>' ] = auth <EOL> self . bootStrap ( headers ) <EOL> if method == '<STR_LIT:POST>' : <EOL> encoded_params = urllib . urlencode ( self . params ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif method == '<STR_LIT>' : <EOL> encoded_params = urllib . urlencode ( self . params ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> if self . params : <EOL> uri = uri , '<STR_LIT:?>' , urllib . urlencode ( self . params ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % method <EOL> print "<STR_LIT>" , self . params <EOL> print "<STR_LIT>" % encoded_params <EOL> print "<STR_LIT>" % ( method , uri ) <EOL> self . makeRequest ( method , uri , encoded_params , headers ) <EOL> response = self . conn . getresponse ( ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % response . status <EOL> return response <EOL> def makeRequest ( self , method , uri , encoded_params , headers ) : <EOL> error_connect = "<STR_LIT>" % ( self . server , self . port ) <EOL> try : <EOL> self . conn . request ( method , uri , encoded_params , headers ) <EOL> except httplib . NotConnected : <EOL> print error_connect <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> except httplib . HTTPException : <EOL> print error_connect <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> except socket . error : <EOL> print error_connect <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> except socket . gaierror : <EOL> print error_connect <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def getJson ( self , data ) : <EOL> return json . loads ( data ) <EOL> def jsonMessage ( self , data ) : <EOL> return json . JSONEncoder ( ) . encode ( data ) <EOL> def restCmd ( self , method , uri , user = '<STR_LIT>' , password = '<STR_LIT>' , opts = { } ) : <EOL> if method == None : <EOL> method = '<STR_LIT:GET>' <EOL> response = self . sendCmd ( method , uri , <EOL> user , password , opts ) <EOL> return self . handleResponse ( method , response , opts ) </s>
