Annoying Socket Problem

John Abel john.abel at pa.press.net
Fri Feb 18 09:27:49 EST 2005


I'm hoping this is something simple, and someone can point me in the 
right direction here.  I have a class based on SocketServer 
(ThreadingTCPServer), and I've used makefile on the socket so I use the 
"for in " routine.  My client sends it a small amount of data.  However, 
both programs appear to hang once the data has been sent, obviously 
something to do with flushing.

I'd appreciate any pointers.

Regards

J

Server Class:

class _DBSocketHandler( SocketServer.BaseRequestHandler ):
   
    xmlStart = re.compile( "XML-START" )
    xmlEnd = re.compile( "XML-END" )
   
    def handle( self ):
        print "Accepted Connection From", self.client_address
        socketIn = self.request.makefile( 'r' )
        socketOut = self.request.makefile( 'wb' )
        remoteDoc = None
        for dataIn in socketIn:
            if self.xmlEnd.match( dataIn ):
                remoteDoc.close()
                break
            if self.xmlStart.match( dataIn ):
                print "Receiving XML"
                remoteDoc = StringIO()
                continue
               
            if remoteDoc is not None:
                remoteDoc.write( dataIn )
               
        socketOut.write( "Got Yer XML File, Thanks" )

Client Code:

    def connect( self ):
        self.socketCon.connect( ( self.dbServer, self.dbPort ) )
        testFile = StringIO.StringIO( testXML )
        self.socketCon.send( "XML-START" )
        for xmlSQL in testFile:
            self.socketCon.send( xmlSQL )
        testFile.close()
        self.socketCon.send( "XML-END" )
        self.socketCon.send( "" )
        time.sleep(10)
        while True:
            dataRec = self.socketCon.recv( 8192 )
            if not dataRec: break
        self.socketCon.close()



More information about the Python-list mailing list