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