Code

Use Python 2.6 bundled SSL when available
authorSven Velt <sven@velt.de>
Tue, 22 Feb 2011 16:46:20 +0000 (17:46 +0100)
committerSven Velt <sven@velt.de>
Wed, 23 Feb 2011 13:58:26 +0000 (14:58 +0100)
Fall back to python-openssl if not found, exit if SSL is required but
both could not be imported.

Signed-off-by: Sven Velt <sven@velt.de>
nagixsc/__init__.py

index 036fe7e16d33ce474909d327ef6782fece7b8510..74c11fffc4690f3c15047f3f2ba431b5bf2a9581 100644 (file)
@@ -574,21 +574,27 @@ def daemonize(pidfile=None, stdin='/dev/null', stdout='/dev/null', stderr='/dev/
 
 class MyHTTPServer(SocketServer.ForkingMixIn, BaseHTTPServer.HTTPServer):
        def __init__(self, server_address, HandlerClass, ssl=False, sslpemfile=None):
+               SocketServer.BaseServer.__init__(self, server_address, HandlerClass)
+
                if ssl:
-                       # FIXME: SSL is in Py2.6
                        try:
-                               from OpenSSL import SSL
+                               import ssl
+                               self.socket = ssl.wrap_socket(socket.socket(self.address_family, self.socket_type), keyfile=sslpemfile, certfile=sslpemfile)
+
                        except:
-                               print 'No Python OpenSSL wrapper/bindings found!'
-                               sys.exit(127)
-
-                       SocketServer.BaseServer.__init__(self, server_address, HandlerClass)
-                       context = SSL.Context(SSL.SSLv23_METHOD)
-                       context.use_privatekey_file (sslpemfile)
-                       context.use_certificate_file(sslpemfile)
-                       self.socket = SSL.Connection(context, socket.socket(self.address_family, self.socket_type))
+
+                               try:
+                                       from OpenSSL import SSL
+                               except:
+                                       print 'No Python SSL or OpenSSL wrapper/bindings found!'
+                                       sys.exit(127)
+
+                               context = SSL.Context(SSL.SSLv23_METHOD)
+                               context.use_privatekey_file (sslpemfile)
+                               context.use_certificate_file(sslpemfile)
+                               self.socket = SSL.Connection(context, socket.socket(self.address_family, self.socket_type))
+
                else:
-                       SocketServer.BaseServer.__init__(self, server_address, HandlerClass)
                        self.socket = socket.socket(self.address_family, self.socket_type)
 
                self.server_bind()