asyncore: handle_accept() question?
Chris
chrisahlers at yahoo.com
Wed Aug 27 17:50:57 EDT 2003
Here's the idea:
I have a listen_server listening to a specific port. A connection
object connects to it remotely, and then the communication begins.
Once connected, I would like to have the server act no differently
than the client (i.e. use the connection class to handle the
connection). This way rather than having a typical server and
client(producer/consumer) I have two peers.
class listen_server(asyncore.dispatcher):
def __init__(self, port): ### Basic Server Stuff ###
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(('', port))
self.listen(5)
def handle_accept(self):
conn, addr = self.accept()
print conn.fileno() ## this always prints a number
print 'connection accepted' ## this always prints
newcon = connection(sock=conn) ## error happens here
class connection(asyncore.dispatcher):
def __init__(self, host = None, port = None, sock = None):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr() # needed?
if host and port: self.connect((host, port))
if sock is not None:
print 'setting socket'
self.set_socket(sock)
def handle_read(self):
data = self.recv(4096)
def handle_write(self):
pass
def handle_connect(self):
print 'connected'
def handle_close(self):
print 'closed connection'
self.close()
Here is my output:
---------------------------
connection accepted
setting socket
Traceback (most recent call last):
File "test.py", line 66, in ?
asyncore.loop()
File "/usr/lib/python2.2/asyncore.py", line 206, in loop
poll_fun (timeout, map)
File "/usr/lib/python2.2/asyncore.py", line 83, in poll
r,w,e = select.select (r,w,e, timeout)
select.error: (9, 'Bad file descriptor')
My basic problem is that I don't know any other way to 'hand off' the
incoming connection - if there is a better way to do this please let
me know. conn.fileno() appears to work, and I don't understand why
this error comes up.
Any ideas?
Thanks,
Chris
More information about the Python-list
mailing list