Question

I created a simple chat server using asynchat module in python. My intention is to make the chat clients wait for a server to be up and running.

I tried doing this using the handle_connect_event by setting connected to True there like:

def handle_connect_event(self):
        self.connected = True

Then I am looping on connect command till connected becomes True:

while not self.connected:
    try:
        self.connect((host, port))
    except:
         time.sleep(1)

I read in the asyncore dispatcher code that when connection is successful, handle_connect_event is called:

def connect(self, address):
    self.connected = False
    err = self.socket.connect_ex(address)
    # XXX Should interpret Winsock return values
    if err in (EINPROGRESS, EALREADY, EWOULDBLOCK):
        return
    if err in (0, EISCONN):
        self.addr = address
        self.handle_connect_event()
    else:
        raise socket.error(err, errorcode[err])

So I believe when the connection is created the code in handle_connect_event should be triggered, thereby setting connected to True, thereby breaking my loop. However this does not happen.

Does anybody know why? And, if this method is wrong, how do we make chat clients wait for server?

I am new to these things, so please explain keeping in mind I am a newbie :)

Was it helpful?

Solution

I guess my machine was crazy for a while but my code works :) I am able to launch 2 client machines, then launch server and get the tasks done.

Best feeling ever ! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top