asyncore/asynchat

F

F.G.Testa

Hi!
Is there a way to access a specific connected socket when using a derived
asyncore.dispatcher and derived asynchat.async_chat?

class AcmeServer(asyncore.dispatcher):
def __init__(self, port):
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind(("", port))
self.listen(5)

def handle_accept(self):
conn, addr = self.accept()
AcmeChannel(self, conn, addr)

def handle_close(self):
print 'Server closed'

def handle_error(self):
print 'Server error'

How do I access the underlying list/map/array of AcmeChannel?
Thank you.
 
J

Jeremy Fincher

F.G.Testa said:
How do I access the underlying list/map/array of AcmeChannel?
Thank you.

There is none, but feel free to keep one yourself; I've done that, for
instance, when I want to limit the number of connections to a server
that should (barring malicious clients) have very fast sessions.

Jeremy
 
F

F.G.Testa

But how do you keep it updated since we don't receive any notifications of
closing/removing channels from asyncore/asynchat?
I've found this: asyncore.socket_map (asyncore.py:62). It is a map of your
inherited asynchat class and I'm using it to search for a specific channel
and send data to it, but having my own dictionary would be more elegant, I
think.
 
J

Jeremy Fincher

F.G.Testa said:
But how do you keep it updated since we don't receive any notifications of
closing/removing channels from asyncore/asynchat?
I've found this: asyncore.socket_map (asyncore.py:62). It is a map of your
inherited asynchat class and I'm using it to search for a specific channel
and send data to it, but having my own dictionary would be more elegant, I
think.

For my purposes I didn't need to keep it updated. Why why do you need
access to such a dictionary? Is the channel itself unable to decide
when it needs to send information?

Jeremy
 
M

MetalOne

A map argument may be passed to asyncore.loop()
asyncore.socket_map constains either a reference to the argument
passed to asyncore.loop() or a global map object.
asyncore.socket_map contains all the connections.

You may also keep track of connections yourself by handling the
callback functions
asyncore.handle_accept()
asyncore.handle_connect()
aysncore.handle_close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top