[Twisted-Python] Application mode: Killing a peer connection from the server in a thread. Is this correct?
Jonathan Sawyer
jon at gina.alaska.edu
Sun Mar 14 01:24:35 EST 2010
Hi everyone,
I am running a server in application context via:
# code: server.tac
factory = Factory()
factory.protocol = MyProtocol
application = service.Application("MyServer")
service = internet.TCPServer(5555, factory)
service.setServiceParent(application)
# end code
ran with
$ twistd -ny server.tac
Here is some of my Protocol code:
# code: myprotocol.py
def checkIdle(transport):
while 1:
now = time.time()
if now - transport.activityFlag > 30.0:
transport.loseConnection()
time.sleep(1)
break
return
time.sleep(8)
return
class MyProtocol(Protocol):
def connectionMade(self):
self.transport.activityFlag = time.time()
thread.start_new_thread(checkIdle, (self.transport,))
# ... the rest of the methods... not important for this question
# end code
Now here's my problem: I want to kill connections from the server that have
been idle for, say, 30 seconds. An idle connection is one in which is a
_valid_ connection (i.e., it hasn't disconnected from the server) but hasn't
sent any data for 30 seconds. The way I have initially set it up was in a
thread. I simply check the last time it had activity from that particular
transport. The really real problem I am having is when the connection is
closed, the client doesn't know about it. It is simply closed on the server
and the client is left hanging. If I test this in telnet with:
$ telnet localhost 5555
telnet never quits with a message: Connection closed by foreign host. (which
is indeed exactly what I want)
The only way I get this response is if I try to send data to the server
while the connection has been "lost".
Can anyone direct me on the proper way to kill a connection from the server
end in a clean manner?
(By the way, I'm very new to twisted, not so new to Python).
Thanks for any help!
--
Jonathan Sawyer
Geographic Information Network of Alaska (GINA)
University of Alaska Fairbanks
(907) 474-5689
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20100313/a9250efd/attachment.htm
More information about the Twisted-Python
mailing list