Asynchat and error handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Orestis Markou

    Asynchat and error handling

    Hello,

    I'm trying to add some better error handling to an async_chat client.
    What I want is to retry or terminate gracefully if the connection to
    the server doesn't succeed. Here's what I have:

    import asyncore, asynchat, socket

    class http_client(asy nchat.async_cha t):

    def __init__(self, host):
    asynchat.async_ chat.__init__(s elf)
    self.create_soc ket(socket.AF_I NET, socket.SOCK_STR EAM)
    terminator = '\x00\xDE\xED\x BE\xEF\x00'
    self.buffer = 'I can haz data?' + terminator
    self._connected = False
    self.set_termin ator(terminator )
    self.connect( (host, 8080) )

    def handle_connect( self):
    print 'connected!'

    def handle_expt(sel f):
    if not self.connected:
    print 'not connected'
    self.close()


    def collect_incomin g_data (self, data):
    print data

    def found_terminato r (self):
    print 'terminator found, Closing'
    self.close()

    def writable(self):
    return (len(self.buffe r) 0)

    def handle_write(se lf):
    sent = self.send(self. buffer)
    self.buffer = self.buffer[sent:]

    if __name__ == '__main__':
    c = http_client('lo calhost')
    asyncore.loop()

    If the connection fails, handle_expt is called, and I can close the
    client. However, I have no information from the error, so I can't
    retry the connection. I would expect the call to "connect" to raise an
    exception, but apparently it's swallowed somewhere. Is there any nice
    way to do this?

    Thanks!

    --
    orestis@orestis .gr

Working...