[Twisted-Python] How to use the TimeoutProtocol policy?
![](https://secure.gravatar.com/avatar/cb15187303e844e25426bd9dd4fd9410.jpg?s=120&d=mm&r=g)
Hi, I'm trying to timeout a really simple LineReceiver protocol. It's function is to connect to a daemon, receive a line that's to be displayed to an end-user upon which the user should undertake some action. The action will trigger a second line (fail/success) at which point the daemon will close the connection. If, however, the end-user does not follow-up on the first challenge I want the script to timeout with a failed result. Now, I have the LineReceiver do exactly what I want, but I can't for the life of me understand how I should "wrap" it in the TimoutProtocol so that it disconnects after a couple of seconds? This is my Factory: class WebSSOFactory(ClientFactory): pamh = None client = None def __init__(self, pamh): self.pamh = pamh def clientConnectionLost(self, connector, reason): reactor.stop() def buildProtocol(self, addr): client = WebSSOClient() #client = TimeoutProtocol(self, WebSSOClient, 5) client.pamh = self.pamh self.client = client return client And this is the protocol: class WebSSOClient(LineReceiver): line = None pamh = None state = 'start' def __init__(self): print("__init__") self.setTimeout(5) def lineReceived(self, line): print("lineReceived") self.line = line if self.state == 'start': self.pamh.conversation(self.pamh.Message(self.pamh.PAM_PROMPT_ECHO_OFF, "Visit http://***/login/%s to login\nand press <enter> to continue." % line)) self.state = None else: self.transport.loseConnection() Before anyone starts to scream http (no s): This is all test/debug. I want the timeout to work before I deploy to something more serious. The client is loaded straightforward using a reactor.connectTCP() with the instantiated factory object. Best regards, Martin -- If 'but' was any useful, it would be a logic operator
![](https://secure.gravatar.com/avatar/c1cd4fcd6c951797d9567954cf2ccca0.jpg?s=120&d=mm&r=g)
Hi Martin, On June 14, 2018 at 8:29:45 AM, Martin van Es (mrvanes@gmail.com) wrote: Hi, I'm trying to timeout a really simple LineReceiver protocol. It's function is to connect to a daemon, receive a line that's to be displayed to an end-user upon which the user should undertake some action. The action will trigger a second line (fail/success) at which point the daemon will close the connection. If, however, the end-user does not follow-up on the first challenge I want the script to timeout with a failed result. Now, I have the LineReceiver do exactly what I want, but I can't for the life of me understand how I should "wrap" it in the TimoutProtocol so that it disconnects after a couple of seconds? This is my Factory: class WebSSOFactory(ClientFactory): pamh = None client = None I think one problem here is that your WebSSOFactory is not a TimeoutFactory (https://twistedmatrix.com/documents/current/api/twisted.protocols.policies.T...). You have to pass a TimeoutFactory to the TimeoutProtocol in order for it to work with the LineReceiver you are wrapping, otherwise the timeout value will not be set; a regular Factory’s buildProtocol method would not know that it needs to pass the timeout to the TimeoutProtocol. Hope this helps, Daniel
![](https://secure.gravatar.com/avatar/c1cd4fcd6c951797d9567954cf2ccca0.jpg?s=120&d=mm&r=g)
Hi Martin, On June 14, 2018 at 8:29:45 AM, Martin van Es (mrvanes@gmail.com) wrote: Hi, I'm trying to timeout a really simple LineReceiver protocol. It's function is to connect to a daemon, receive a line that's to be displayed to an end-user upon which the user should undertake some action. The action will trigger a second line (fail/success) at which point the daemon will close the connection. If, however, the end-user does not follow-up on the first challenge I want the script to timeout with a failed result. Now, I have the LineReceiver do exactly what I want, but I can't for the life of me understand how I should "wrap" it in the TimoutProtocol so that it disconnects after a couple of seconds? This is my Factory: class WebSSOFactory(ClientFactory): pamh = None client = None I think one problem here is that your WebSSOFactory is not a TimeoutFactory (https://twistedmatrix.com/documents/current/api/twisted.protocols.policies.T...). You have to pass a TimeoutFactory to the TimeoutProtocol in order for it to work with the LineReceiver you are wrapping, otherwise the timeout value will not be set; a regular Factory’s buildProtocol method would not know that it needs to pass the timeout to the TimeoutProtocol. Hope this helps, Daniel
participants (2)
-
L. Daniel Burr
-
Martin van Es