From f31463596fd7379c42c6b746919f3e3bd88e4baa Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Tue, 22 Feb 2011 17:46:20 +0100 Subject: [PATCH] Use Python 2.6 bundled SSL when available Fall back to python-openssl if not found, exit if SSL is required but both could not be imported. Signed-off-by: Sven Velt --- nagixsc/__init__.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/nagixsc/__init__.py b/nagixsc/__init__.py index 036fe7e..74c11ff 100644 --- a/nagixsc/__init__.py +++ b/nagixsc/__init__.py @@ -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() -- 2.30.2