<s> import jsonrpclib <EOL> from jsonrpclib import Fault <EOL> from jsonrpclib . jsonrpc import USE_UNIX_SOCKETS <EOL> import SimpleXMLRPCServer <EOL> import SocketServer <EOL> import socket <EOL> import logging <EOL> import os <EOL> import types <EOL> import traceback <EOL> import sys <EOL> try : <EOL> import fcntl <EOL> except ImportError : <EOL> fcntl = None <EOL> def get_version ( request ) : <EOL> if '<STR_LIT>' in request . keys ( ) : <EOL> return <NUM_LIT> <EOL> if '<STR_LIT:id>' in request . keys ( ) : <EOL> return <NUM_LIT:1.0> <EOL> return None <EOL> def validate_request ( request ) : <EOL> if not isinstance ( request , dict ) : <EOL> fault = Fault ( <EOL> - <NUM_LIT> , '<STR_LIT>' % type ( request ) <EOL> ) <EOL> return fault <EOL> rpcid = request . get ( '<STR_LIT:id>' , None ) <EOL> version = get_version ( request ) <EOL> if not version : <EOL> fault = Fault ( - <NUM_LIT> , '<STR_LIT>' % request , rpcid = rpcid ) <EOL> return fault <EOL> request . setdefault ( '<STR_LIT>' , [ ] ) <EOL> method = request . get ( '<STR_LIT>' , None ) <EOL> params = request . get ( '<STR_LIT>' ) <EOL> param_types = ( types . ListType , types . DictType , types . TupleType ) <EOL> if not method or type ( method ) not in types . StringTypes or type ( params ) not in param_types : <EOL> fault = Fault ( <EOL> - <NUM_LIT> , '<STR_LIT>' , rpcid = rpcid <EOL> ) <EOL> return fault <EOL> return True <EOL> class SimpleJSONRPCDispatcher ( SimpleXMLRPCServer . SimpleXMLRPCDispatcher ) : <EOL> def __init__ ( self , encoding = None ) : <EOL> SimpleXMLRPCServer . SimpleXMLRPCDispatcher . __init__ ( <EOL> self , allow_none = True , encoding = encoding ) <EOL> def _marshaled_dispatch ( self , data , dispatch_method = None ) : <EOL> response = None <EOL> try : <EOL> request = jsonrpclib . loads ( data ) <EOL> except Exception , e : <EOL> fault = Fault ( - <NUM_LIT> , '<STR_LIT>' % ( data , e ) ) <EOL> response = fault . response ( ) <EOL> return response <EOL> if not request : <EOL> fault = Fault ( - <NUM_LIT> , '<STR_LIT>' ) <EOL> return fault . response ( ) <EOL> if isinstance ( request , list ) : <EOL> responses = [ ] <EOL> for req_entry in request : <EOL> result = validate_request ( req_entry ) <EOL> if type ( result ) is Fault : <EOL> responses . append ( result . response ( ) ) <EOL> continue <EOL> resp_entry = self . _marshaled_single_dispatch ( req_entry ) <EOL> if resp_entry is not None : <EOL> responses . append ( resp_entry ) <EOL> if len ( responses ) > <NUM_LIT:0> : <EOL> response = '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( responses ) <EOL> else : <EOL> response = '<STR_LIT>' <EOL> else : <EOL> result = validate_request ( request ) <EOL> if type ( result ) is Fault : <EOL> return result . response ( ) <EOL> response = self . _marshaled_single_dispatch ( request ) <EOL> return response <EOL> def _marshaled_single_dispatch ( self , request ) : <EOL> method = request . get ( '<STR_LIT>' ) <EOL> params = request . get ( '<STR_LIT>' ) <EOL> try : <EOL> response = self . _dispatch ( method , params ) <EOL> except : <EOL> exc_type , exc_value , exc_tb = sys . exc_info ( ) <EOL> fault = Fault ( - <NUM_LIT> , '<STR_LIT>' % ( exc_type , exc_value ) ) <EOL> return fault . response ( ) <EOL> if '<STR_LIT:id>' not in request . keys ( ) or request [ '<STR_LIT:id>' ] is None : <EOL> return None <EOL> try : <EOL> response = jsonrpclib . dumps ( response , <EOL> methodresponse = True , <EOL> rpcid = request [ '<STR_LIT:id>' ] <EOL> ) <EOL> return response <EOL> except : <EOL> exc_type , exc_value , exc_tb = sys . exc_info ( ) <EOL> fault = Fault ( - <NUM_LIT> , '<STR_LIT>' % ( exc_type , exc_value ) ) <EOL> return fault . response ( ) <EOL> def _dispatch ( self , method , params ) : <EOL> func = None <EOL> try : <EOL> func = self . funcs [ method ] <EOL> except KeyError : <EOL> if self . instance is not None : <EOL> if hasattr ( self . instance , '<STR_LIT>' ) : <EOL> return self . instance . _dispatch ( method , params ) <EOL> else : <EOL> try : <EOL> func = SimpleXMLRPCServer . resolve_dotted_attribute ( <EOL> self . instance , <EOL> method , <EOL> True <EOL> ) <EOL> except AttributeError : <EOL> pass <EOL> if func is not None : <EOL> try : <EOL> if isinstance ( params , types . ListType ) : <EOL> response = func ( * params ) <EOL> else : <EOL> response = func ( ** params ) <EOL> return response <EOL> except : <EOL> err_lines = traceback . format_exc ( ) . splitlines ( ) <EOL> trace_string = '<STR_LIT>' % ( err_lines [ - <NUM_LIT:3> ] , err_lines [ - <NUM_LIT:1> ] ) <EOL> fault = jsonrpclib . Fault ( - <NUM_LIT> , '<STR_LIT>' % <EOL> trace_string ) <EOL> return fault <EOL> else : <EOL> return Fault ( - <NUM_LIT> , '<STR_LIT>' % method ) <EOL> class SimpleJSONRPCRequestHandler ( <EOL> SimpleXMLRPCServer . SimpleXMLRPCRequestHandler ) : <EOL> def do_POST ( self ) : <EOL> if not self . is_rpc_path_valid ( ) : <EOL> self . report_404 ( ) <EOL> return <EOL> try : <EOL> max_chunk_size = <NUM_LIT:10> * <NUM_LIT> * <NUM_LIT> <EOL> size_remaining = int ( self . headers [ "<STR_LIT>" ] ) <EOL> L = [ ] <EOL> while size_remaining : <EOL> chunk_size = min ( size_remaining , max_chunk_size ) <EOL> L . append ( self . rfile . read ( chunk_size ) ) <EOL> size_remaining -= len ( L [ - <NUM_LIT:1> ] ) <EOL> data = '<STR_LIT>' . join ( L ) <EOL> response = self . server . _marshaled_dispatch ( data ) <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> except Exception : <EOL> self . send_response ( <NUM_LIT> ) <EOL> err_lines = traceback . format_exc ( ) . splitlines ( ) <EOL> trace_string = '<STR_LIT>' % ( err_lines [ - <NUM_LIT:3> ] , err_lines [ - <NUM_LIT:1> ] ) <EOL> fault = jsonrpclib . Fault ( - <NUM_LIT> , '<STR_LIT>' % trace_string ) <EOL> response = fault . response ( ) <EOL> if response is None : <EOL> response = '<STR_LIT>' <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . send_header ( "<STR_LIT>" , str ( len ( response ) ) ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( response ) <EOL> self . wfile . flush ( ) <EOL> self . connection . shutdown ( <NUM_LIT:1> ) <EOL> class SimpleJSONRPCServer ( SocketServer . TCPServer , SimpleJSONRPCDispatcher ) : <EOL> allow_reuse_address = True <EOL> def __init__ ( self , addr , requestHandler = SimpleJSONRPCRequestHandler , <EOL> logRequests = True , encoding = None , bind_and_activate = True , <EOL> address_family = socket . AF_INET ) : <EOL> self . logRequests = logRequests <EOL> SimpleJSONRPCDispatcher . __init__ ( self , encoding ) <EOL> vi = sys . version_info <EOL> self . address_family = address_family <EOL> if USE_UNIX_SOCKETS and address_family == socket . AF_UNIX : <EOL> if os . path . exists ( addr ) : <EOL> try : <EOL> os . unlink ( addr ) <EOL> except OSError : <EOL> logging . warning ( "<STR_LIT>" , addr ) <EOL> if vi [ <NUM_LIT:0> ] < <NUM_LIT:3> and vi [ <NUM_LIT:1> ] < <NUM_LIT:6> : <EOL> SocketServer . TCPServer . __init__ ( self , addr , requestHandler ) <EOL> else : <EOL> SocketServer . TCPServer . __init__ ( <EOL> self , addr , requestHandler , bind_and_activate ) <EOL> if fcntl is not None and hasattr ( fcntl , '<STR_LIT>' ) : <EOL> flags = fcntl . fcntl ( self . fileno ( ) , fcntl . F_GETFD ) <EOL> flags |= fcntl . FD_CLOEXEC <EOL> fcntl . fcntl ( self . fileno ( ) , fcntl . F_SETFD , flags ) <EOL> class CGIJSONRPCRequestHandler ( SimpleJSONRPCDispatcher ) : <EOL> def __init__ ( self , encoding = None ) : <EOL> SimpleJSONRPCDispatcher . __init__ ( self , encoding ) <EOL> def handle_jsonrpc ( self , request_text ) : <EOL> response = self . _marshaled_dispatch ( request_text ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % len ( response ) <EOL> print <EOL> sys . stdout . write ( response ) <EOL> handle_xmlrpc = handle_jsonrpc </s>
