1 # XMLRPC-specific code for SFA Client
3 # starting with 2.7.9 we need to turn off server verification
6 turn_off_server_verify = {'context': ssl._create_unverified_context()}
8 turn_off_server_verify = {}
10 from sfa.util.py23 import xmlrpc_client
11 from sfa.util.py23 import http_client
14 from sfa.util.sfalogging import logger
17 logger = logging.getLogger('sfaserverproxy')
20 # ServerException, ExceptionUnmarshaller
22 # Used to convert server exception strings back to an exception.
23 # from usenet, Raghuram Devarakonda
26 class ServerException(Exception):
30 class ExceptionUnmarshaller(xmlrpc_client.Unmarshaller):
34 return xmlrpc_client.Unmarshaller.close(self)
35 except xmlrpc_client.Fault as e:
36 raise ServerException(e.faultString)
41 # A transport for XMLRPC that works on top of HTTPS
43 # targetting only python-2.7 we can get rid of some older code
46 class XMLRPCTransport(xmlrpc_client.Transport):
48 def __init__(self, key_file=None, cert_file=None, timeout=None):
49 xmlrpc_client.Transport.__init__(self)
50 self.timeout = timeout
51 self.key_file = key_file
52 self.cert_file = cert_file
54 def make_connection(self, host):
55 # create a HTTPS connection object from a host descriptor
56 # host may be a string, or a (host, x509-dict) tuple
57 host, extra_headers, x509 = self.get_host_info(host)
58 conn = http_client.HTTPSConnection(host, None, key_file=self.key_file,
59 cert_file=self.cert_file,
60 **turn_off_server_verify)
62 # Some logic to deal with timeouts. It appears that some (or all) versions
63 # of python don't set the timeout after the socket is created. We'll do it
64 # ourselves by forcing the connection to connect, finding the socket, and
65 # calling settimeout() on it. (tested with python 2.6)
67 if hasattr(conn, 'set_timeout'):
68 conn.set_timeout(self.timeout)
70 if hasattr(conn, "_conn"):
71 # HTTPS is a wrapper around HTTPSConnection
72 real_conn = conn._conn
76 if hasattr(real_conn, "sock") and hasattr(real_conn.sock, "settimeout"):
77 real_conn.sock.settimeout(float(self.timeout))
82 unmarshaller = ExceptionUnmarshaller()
83 parser = xmlrpc_client.ExpatParser(unmarshaller)
84 return parser, unmarshaller
87 class XMLRPCServerProxy(xmlrpc_client.ServerProxy):
89 def __init__(self, url, transport, allow_none=True, verbose=False):
90 # remember url for GetVersion
91 # xxx not sure this is still needed as SfaServerProxy has this too
93 xmlrpc_client.ServerProxy.__init__(self, url, transport, allow_none=allow_none,
95 **turn_off_server_verify)
97 def __getattr__(self, attr):
98 logger.debug("xml-rpc %s method:%s" % (self.url, attr))
99 return xmlrpc_client.ServerProxy.__getattr__(self, attr)
101 # the object on which we can send methods that get sent over xmlrpc
104 class SfaServerProxy:
106 def __init__(self, url, keyfile, certfile, verbose=False, timeout=None):
108 self.keyfile = keyfile
109 self.certfile = certfile
110 self.verbose = verbose
111 self.timeout = timeout
112 # an instance of xmlrpc_client.ServerProxy
113 transport = XMLRPCTransport(keyfile, certfile, timeout)
114 self.serverproxy = XMLRPCServerProxy(
115 url, transport, allow_none=True, verbose=verbose)
117 # this is python magic to return the code to run when
118 # SfaServerProxy receives a method call
119 # so essentially we send the same method with identical arguments
120 # to the server_proxy object
121 def __getattr__(self, name):
122 def func(*args, **kwds):
123 return getattr(self.serverproxy, name)(*args, **kwds)