[Twisted-Python] How to wait for a transport to write some data before loosing the connection

emmanuel CAZENAVE emmanuel.cazenave at gmail.com
Tue Jun 28 05:45:08 EDT 2011


Hi,

I wrote a server using a protocol that inherit from 'basic.LineReceiver'.

In some circonstances I need  to reject the data send by the client and to
close the connection. Here is how I do that:

class MyProtocol(basic.LineReceiver):

    ........

    def error(self, error_msg):
        self.transport.write(error_msg)
        self.transport.loseConnection()


Sometimes it works: the client gets notify of the error and the connection
is closed properly.
And some other times, the client does not received the error notification
and the server closes the connection, which causes the client (wich is not
written using twisted, but with the socket module from the standard lib) to
crash with 'socket.error: (10054, 'Connection reset by peer')'.

As I red somewhere in the docs or in the mailing list, self.transort.write
does can return before it really writes the data.

So how could I do, without using blocking code, to wait
until self.transort.write effectively writes the data to avoid closing the
connection with unwritten data ?

Thanks you.

Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20110628/ccabaa73/attachment.htm 


More information about the Twisted-Python mailing list