tkinter asyncore.loop

Rune Hansen rune.hansen at viventus.no
Tue Sep 17 07:28:37 EDT 2002


Hello, Some months back I sendt a question to the list where I asked
if anyone knew how to catch errors in an asyncore loop.
I got an answer to override the "handle_error" in the client. I
still can't get it to trap any errors though. The error I need to trap is
"Connection lost". If I remove the network connection and try to
update the stream or post to the socket nothing happens. When I
reconect the network my application just continues to serve like
nothing has happened. This is of course nice, but not the
functionallity I'm looking for. 
Here's the code I'm working with:

class StreamApp(StreamAppTK):
    def __init__(self):
        ...
    def streamOn(self):
        ...
        self.sc = client(host[0],int(port[0]),self,self.readPasswordFile())
        self.poll()
    def poll(self):
        try:
            asyncore.poll(0.05)
            if self.sc.connected:
                self.master.after(100, self.poll)
        except:
            pass   

class client(asyncore.dispatcher):
    def __init__(self,host,port,StreamApp,userPwd):
        asyncore.dispatcher.__init__(self)
        try:
            self.host = host
            self.port = port
            self.StreamApp = StreamApp
            self.fp = ""                
            username, password = userPwd
            self.buffer = "login "+username+" 
"+md5.new(password).digest()+"\n"
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.connect( (self.host, self.port) )
        except socket.error,errMsg:
            self.StreamApp.messageBar.message('state','Error %s, %s' % 
(errMsg))
    def handle_connect(self):
        pass
    def handle_read(self):
        try:
            self.fp = self.fp + self.recv(65565)
            if "\n" in self.fp:
                tmp = self.fp.split("\n")[:]
                for i in tmp[:-1]:
                    self.StreamApp.insertItem(i)
                self.fp = tmp[len(tmp)-1]
        except socket.error,errMsg:
            self.StreamApp.messageBar.message('state','Error %s, %s' % 
(errMsg))
    def writeable (self):
        return (len(self.buffer) > 0)
    def handle_write(self,buffer=""):
        try:
            if buffer:
                self.buffer = buffer
            sent = self.send(self.buffer)
            self.buffer = self.buffer[sent:]
        except socket.error,errMsg:
            self.StreamApp.messageBar.message('state','Error %s, %s' % 
(errMsg))
    def handle_error(self, etype, evalue, etb):
        print 'handling error: %s, %s, %s' % (etype, evalue, etb)
    def handle_close(self):
        self.connected=0
        self.close()
        

As always I'll apreciate any help...

regards

/rune




More information about the Python-list mailing list