By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Asynchat and error handling

P: n/a
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(asynchat.async_chat):

def __init__(self, host):
asynchat.async_chat.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
terminator = '\x00\xDE\xED\xBE\xEF\x00'
self.buffer = 'I can haz data?' + terminator
self._connected = False
self.set_terminator(terminator)
self.connect( (host, 8080) )

def handle_connect(self):
print 'connected!'

def handle_expt(self):
if not self.connected:
print 'not connected'
self.close()
def collect_incoming_data (self, data):
print data

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

def writable(self):
return (len(self.buffer) 0)

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

if __name__ == '__main__':
c = http_client('localhost')
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!

--
or*****@orestis.gr
http://orestis.gr
Nov 5 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.