asyncore: handle_accept() question?

Chris chrisahlers at
Wed Aug 27 23:50:57 CEST 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 ###
		self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
		self.bind(('', port))

	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):
		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'

	def handle_read(self):
		data = self.recv(4096)
	def handle_write(self):

	def handle_connect(self):
		print 'connected'

	def handle_close(self):
		print 'closed connection'

Here is my output:
connection accepted
setting socket
Traceback (most recent call last):
  File "", line 66, in ?
  File "/usr/lib/python2.2/", line 206, in loop
    poll_fun (timeout, map)
  File "/usr/lib/python2.2/", line 83, in poll
    r,w,e = (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?


More information about the Python-list mailing list