[Python-Dev] ssl module integration with asyncore (original) (raw)

Giampaolo Rodola' gnewsg at gmail.com
Wed Nov 28 03:19:58 CET 2007


I tried to write a simple asyncore-based server code, then I used a simple client to establish a connection with it. Once the client is connected server raises the following exception:

--- snippet --- C:\Documents and Settings\billiejoex\Desktop\test>test.py []127.0.0.1:3003 Connected. Traceback (most recent call last): File "C:\Documents and Settings\billiejoex\Desktop\test\test.py", line 40, in asyncore.loop(timeout=1) File "C:\Python26\lib\asyncore.py", line 191, in loop poll_fun(timeout, map) File "C:\Python26\lib\asyncore.py", line 132, in poll read(obj) File "C:\Python26\lib\asyncore.py", line 72, in read obj.handle_error() File "C:\Python26\lib\asyncore.py", line 68, in read obj.handle_read_event() File "C:\Python26\lib\asyncore.py", line 384, in handle_read_event self.handle_accept() File "C:\Documents and Settings\billiejoex\Desktop\test\test.py", line 33, in handle_accept Handler(sock_obj) File "C:\Documents and Settings\billiejoex\Desktop\test\test.py", line 9, in _ init_ certfile='keycert.pem') File "C:\Python26\lib\ssl.py", line 466, in wrap_socket ssl_version=ssl_version, ca_certs=ca_certs) File "C:\Python26\lib\ssl.py", line 103, in init cert_reqs, ssl_version, ca_certs) ssl.SSLError: [Errno 2] _ssl.c:429: The operation did not complete (read) --- /snippet ---

This is the server code I used. Note that 'keycert.pem' is the certificate file I found in the test directory.

--- snippet --- import asyncore, asynchat, socket, ssl

class Handler(asyncore.dispatcher):

def __init__(self, conn):
    asyncore.dispatcher.__init__(self, conn)
    self.socket = ssl.wrap_socket(conn, server_side=True,
                                  certfile='keycert.pem')
    self.send('hi there')

def readable(self):
    if isinstance(self.socket, ssl.SSLSocket):
        while self.socket.pending() > 0:
            self.handle_read_event()
    return True

def handle_error(self):
    raise

class Server(asyncore.dispatcher):

def __init__(self):
    asyncore.dispatcher.__init__(self)
    self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
    self.bind(('', 54321))
    self.listen(5)

def handle_accept(self):
    sock_obj, addr = self.accept()
    print "[]%s:%s Connected." %addr
    Handler(sock_obj)

def handle_error(self):
    raise

Server() asyncore.loop(timeout=1) --- snippet ---



More information about the Python-Dev mailing list