remote connections timeout after 30 seconds
[sfa.git] / sfa / util / httpsProtocol.py
1 import httplib
2 import socket
3
4 # wrapper around standartd https modules. Properly supports timeouts.  
5
6 class HTTPSConnection(httplib.HTTPSConnection):
7     def __init__(self, host, port=None, key_file=None, cert_file=None,
8                  strict=None, timeout = None):
9         httplib.HTTPSConnection.__init__(self, host, port, key_file, cert_file, strict)
10         self.timeout = float(timeout)
11
12     def connect(self):
13         """Connect to a host on a given (SSL) port."""
14         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
15         sock.settimeout(self.timeout)
16         sock.connect((self.host, self.port))
17         ssl = socket.ssl(sock, self.key_file, self.cert_file)
18         self.sock = httplib.FakeSocket(sock, ssl)
19
20 class HTTPS(httplib.HTTPS):
21    def __init__(self, host='', port=None, key_file=None, cert_file=None,
22                      strict=None, timeout = None):
23         # urf. compensate for bad input.
24         if port == 0:
25             port = None
26         self._setup(HTTPSConnection(host, port, key_file, cert_file, strict, timeout))
27
28         # we never actually use these for anything, but we keep them
29         # here for compatibility with post-1.5.2 CVS.
30         self.key_file = key_file
31         self.cert_file = cert_file
32