[Pythonmac-SIG] Lost connection

broz broz@broznews.org
Sun, 28 Apr 2002 00:53:46 -0400


Hi folks,

The solution to my problem could very well seem obvious, but to me it's not
obvious.

I've got a python application that opens a socket, receives data (XML),
parses it, and updates a database. Under normal circumstances, it all works
just fine. Under abnormal circumstances, it sits and spins.

The main application opens a socket with listen(), and then accept() is
called. It dutifully waits for the other machine to connect() and start
dumping data. That process works fine. If the other machine drops the line,
things get hairy. My python app thinks the connection is still live, and
spins waiting for more data. I'm just doing a socket.recv(2048) to get the
data. I would have expected an error to be thrown if the connection failed,
but no error is thrown.

Odd.

What I need to do is discover the connection went down, and open a new one
when the server on the other end comes back.

I'm running this on Mac OS X. How can I tell the connection went down?

Thanks in advance.


def socketSetup(self, thePort):
        
        try:
            self.theSock = socket(AF_INET, SOCK_STREAM)

            self.theSock.bind(('', thePort))
            self.theSock.listen(5)
        except Exception, theErr:
            self.logger.logStatus( "connection failed: " + str(theErr))
            
    def socketConnect(self):
        " this is where we accept the connection from the server"
        print "in socketConnect"
        try: 
            if not self.theSock:
                print "theSock is None!"
            else:
                self.theConnection, theAddress = self.theSock.accept()
                self.connected = 1

        except socket.error, theErr:
            self.connected = 0
            print "socketConnect error - " + str(error) + ": " + str(theErr)
        
    def getConnectStatus(self):
        return self.connected
         
    def getData(self):
        "handle data feeds from either a file or a socket"
        # print "getData called"
        try:
            if self.connected:
                   
                self.theData = self.theConnection.recv(2048)
            else:
                self.socketConnect()
                
            return self.theData
        except Exception, theErr:
            print ("getData error: " + theErr)
            self.logger.log("getData error: " + theErr)
                
            
 
-- 

Richard Brosnahan
Editor in Chief
Broz News
Hand Picked Technical News
Updated Every Business Day!
http://broznews.com/