1 # XMLRPC-specific code for SFA Client
4 from httplib import HTTPS, HTTPSConnection
7 from sfa.util.sfalogging import logger
10 logger=logging.getLogger('sfaprotocol')
13 # ServerException, ExceptionUnmarshaller
15 # Used to convert server exception strings back to an exception.
16 # from usenet, Raghuram Devarakonda
18 class ServerException(Exception):
21 class ExceptionUnmarshaller(xmlrpclib.Unmarshaller):
24 return xmlrpclib.Unmarshaller.close(self)
25 except xmlrpclib.Fault, e:
26 raise ServerException(e.faultString)
31 # A transport for XMLRPC that works on top of HTTPS
33 # python 2.7 xmlrpclib has changed its internal code
34 # it now calls 'getresponse' on the obj returned by make_connection
35 # while it used to call 'getreply'
36 # regardless of the version, httplib.HTTPS does implement getreply,
37 # while httplib.HTTPSConnection has getresponse
38 # so we create a dummy instance to check what's expected
39 need_HTTPSConnection=hasattr(xmlrpclib.Transport().make_connection('localhost'),'getresponse')
41 class XMLRPCTransport(xmlrpclib.Transport):
43 def __init__(self, key_file=None, cert_file=None, timeout=None):
44 xmlrpclib.Transport.__init__(self)
46 self.key_file = key_file
47 self.cert_file = cert_file
49 def make_connection(self, host):
50 # create a HTTPS connection object from a host descriptor
51 # host may be a string, or a (host, x509-dict) tuple
52 host, extra_headers, x509 = self.get_host_info(host)
53 if need_HTTPSConnection:
54 #conn = HTTPSConnection(host, None, key_file=self.key_file, cert_file=self.cert_file, timeout=self.timeout) #**(x509 or {}))
55 conn = HTTPSConnection(host, None, key_file=self.key_file, cert_file=self.cert_file) #**(x509 or {}))
57 #conn = HTTPS(host, None, key_file=self.key_file, cert_file=self.cert_file, timeout=self.timeout) #**(x509 or {}))
58 conn = HTTPS(host, None, key_file=self.key_file, cert_file=self.cert_file) #**(x509 or {}))
60 if hasattr(conn, 'set_timeout'):
61 conn.set_timeout(self.timeout)
63 # Some logic to deal with timeouts. It appears that some (or all) versions
64 # of python don't set the timeout after the socket is created. We'll do it
65 # ourselves by forcing the connection to connect, finding the socket, and
66 # calling settimeout() on it. (tested with python 2.6)
68 if hasattr(conn, "_conn"):
69 # HTTPS is a wrapper around HTTPSConnection
70 real_conn = conn._conn
74 if hasattr(real_conn, "sock") and hasattr(real_conn.sock, "settimeout"):
75 real_conn.sock.settimeout(float(self.timeout))
80 unmarshaller = ExceptionUnmarshaller()
81 parser = xmlrpclib.ExpatParser(unmarshaller)
82 return parser, unmarshaller
84 class XMLRPCServerProxy(xmlrpclib.ServerProxy):
85 def __init__(self, url, transport, allow_none=True, verbose=False):
86 # remember url for GetVersion
88 xmlrpclib.ServerProxy.__init__(self, url, transport, allow_none=allow_none, verbose=verbose)
90 def __getattr__(self, attr):
91 logger.debug ("xml-rpc %s method:%s"%(self.url,attr))
92 return xmlrpclib.ServerProxy.__getattr__(self, attr)
94 def server_proxy(url, key_file, cert_file, timeout=None, verbose=False):
95 transport = XMLRPCTransport(key_file, cert_file, timeout)
96 return XMLRPCServerProxy(url, transport, allow_none=True, verbose=verbose)