summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2016-07-12 19:41:48 +0200
committerMatěj Cepl <mcepl@cepl.eu>2016-07-19 19:47:51 +0200
commit8dfef705651cb284a0cb4348c14086e5d54268d3 (patch)
treeb7138c5c62d9adb2ea390eba2084577efc9488ba /contrib
parent71d87c3025f3fe16af41f2014219b96be6524026 (diff)
downloadm2crypto-8dfef705651cb284a0cb4348c14086e5d54268d3.tar.gz
contrib/dispatcher.py: Fix M2Crypto imports.
Also fix zillion of PEP8 issues.
Diffstat (limited to 'contrib')
-rw-r--r--contrib/dispatcher.py116
1 files changed, 49 insertions, 67 deletions
diff --git a/contrib/dispatcher.py b/contrib/dispatcher.py
index 8e74985..b0ee58f 100644
--- a/contrib/dispatcher.py
+++ b/contrib/dispatcher.py
@@ -1,25 +1,24 @@
#!/usr/local/bin/python -O
-
from __future__ import print_function
-
-"""
- Implements a [hopefully] non-blocking SSL dispatcher on top of
- M2Crypto package.
+"""Implements a [hopefully] non-blocking SSL Dispatcher on top of M2Crypto.
Written by Ilya Etingof <ilya@glas.net>, 05/2001
"""
-import asyncore, socket
+import asyncore # type: ignore # https://github.com/python/typeshed/issues/356
+import socket
# M2Crypto
-from M2Crypto import SSL
+from M2Crypto import SSL # type: ignore # we are not in proper directory
-class _nb_connection (SSL.Connection):
- """Functional equivalent of SSL.Connection class. Facilitates
- possibly delayed socket.connect() and socket.accept()
+
+class NBConnection(SSL.Connection):
+ """Functional equivalent of SSL.Connection class.
+
+ Facilitates possibly delayed socket.connect() and socket.accept()
termination.
"""
- def __init__ (self, ctx, sock):
- SSL.Connection.__init__ (self, ctx, sock)
+ def __init__(self, ctx, sock):
+ SSL.Connection.__init__(self, ctx, sock)
def connect(self, addr):
self._setup_ssl(addr)
@@ -29,12 +28,11 @@ class _nb_connection (SSL.Connection):
self._setup_ssl(addr)
self.accept_ssl()
-class dispatcher(asyncore.dispatcher_with_send):
- """A non-blocking SSL dispatcher that mimics the
- asyncode.dispatcher API.
- """
- def __init__ (self, cert, key, sock=None, serving=None):
- asyncore.dispatcher_with_send.__init__ (self)
+
+class Dispatcher(asyncore.dispatcher_with_send):
+ """A non-blocking SSL Dispatcher that mimics the asyncode.dispatcher API"""
+ def __init__(self, cert, key, sock=None, serving=None):
+ asyncore.dispatcher_with_send.__init__(self)
self.__serving = serving
@@ -43,14 +41,15 @@ class dispatcher(asyncore.dispatcher_with_send):
if self.__serving:
self.set_socket(sock)
else:
- self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
+ self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.ctx = SSL.Context()
self.ctx.set_verify(SSL.verify_none, 10)
self.ctx.load_cert(cert, key)
self.ctx.set_info_callback()
- self.ssl = _nb_connection(self.ctx, self.socket)
+ self.ssl = NBConnection(self.ctx, self.socket)
+ self.peer = None
self.__output = ''
self.__want_write = 1
@@ -59,24 +58,22 @@ class dispatcher(asyncore.dispatcher_with_send):
# The following are asyncore overloaded methods
#
- def handle_connect (self):
- """Initiate SSL connection negotiation
- """
+ def handle_connect(self):
+ """Initiate SSL connection negotiation"""
if self.__serving:
- self.ssl.accept (self.addr)
+ self.ssl.accept(self.addr)
self.peer = self.ssl.get_peer_cert()
self.handle_ssl_accept()
else:
- self.ssl.connect (self.addr)
+ self.ssl.connect(self.addr)
self.handle_ssl_connect()
def handle_read(self):
- """Read user and/or SSL protocol data from SSL connection
- """
+ """Read user and/or SSL protocol data from SSL connection"""
ret = self.ssl._read_nbio()
if ret:
@@ -86,9 +83,7 @@ class dispatcher(asyncore.dispatcher_with_send):
self.__want_write = 1
def handle_write(self):
- """Write pending user and/or SSL protocol data down to SSL
- connection
- """
+ """Write pending user or SSL protocol data down to SSL connection"""
self.__want_write = 0
ret = self.ssl._write_nbio(self.__output)
@@ -105,28 +100,27 @@ class dispatcher(asyncore.dispatcher_with_send):
else:
self.__output = self.__output[ret:]
- def writable (self):
- """Indicate that write is desired if here're some
- user and/or SSL protocol data.
+ def writable(self):
+ """Indicate that write is desired.
+
+ Happens if there's some user and/or SSL protocol data.
"""
if self.__output or self.__want_write:
return 1
return self.ssl_writable()
- def handle_close (self):
- """Shutdown SSL connection.
- """
+ def handle_close(self):
+ """Shutdown SSL connection."""
self.ssl = None
self.ctx = None
- self.close ()
+ self.close()
self.handle_ssl_close()
- def handle_error (self, *info):
- """A trap for asyncore errors
- """
+ def handle_error(self, *info):
+ """A trap for asyncore errors"""
self.handle_ssl_error(info)
#
@@ -134,61 +128,49 @@ class dispatcher(asyncore.dispatcher_with_send):
#
def ssl_connect(self, server):
- """Initiate SSL connection
- """
+ """Initiate SSL connection"""
self.connect(server)
def ssl_write(self, data):
- """Write data to SSL connection
- """
+ """Write data to SSL connection"""
self.__output = self.__output + data
def ssl_close(self):
- """Close SSL connection
- """
+ """Close SSL connection"""
self.handle_close()
def handle_ssl_connect(self):
- """Invoked on SSL connection establishment (whilst
- in client mode)
- """
+ """Invoked on SSL connection establishment (whilst in Client mode)"""
print('Unhandled handle_ssl_connect()')
def handle_ssl_accept(self):
- """Invoked on SSL connection establishment (whilst
- in server mode)
- """
+ """Invoked on SSL connection establishment (whilst in server mode)"""
print('Unhandled handle_ssl_accept()')
def handle_ssl_read(self, data):
- """Invoked on new data arrival to SSL connection
- """
+ """Invoked on new data arrival to SSL connection"""
print('Unhandled handle_ssl_read event')
def handle_ssl_close(self):
- """Invoked on SSL connection termination
- """
+ """Invoked on SSL connection termination"""
pass
def ssl_writable(self):
- """Invoked prior to every select() call
- """
+ """Invoked prior to every select() call"""
return 0
-if __name__=='__main__':
- """Give it a test run
- """
- class client(dispatcher):
- """SSL client class
- """
- def __init__ (self, cert, key):
- dispatcher.__init__(self, cert, key)
+if __name__ == '__main__':
+ """Give it a test run"""
+ class Client(Dispatcher):
+ """SSL Client class"""
+ def __init__(self, cert, key):
+ Dispatcher.__init__(self, cert, key)
def handle_ssl_read(self, data):
print(data)
self.ssl_write('test write')
- ssl = client('test.cert', 'test.key')
+ ssl = Client('test.cert', 'test.key')
ssl.ssl_connect(('localhost', 7777))
asyncore.loop()