1

I'm creating TCP client socket with Twisted. I need to check connection status in a loop interval in connectionMade method.

from twisted.internet import reactor, protocol

class ClientProtocol(protocol.Protocol):
    def connectionMade(self):
       while not thread_obj.stopped.wait(10):
            print ('ping')
            self.transport.write(b'test') # Byte value

For check connection losing, i manually disconnect my network And I checked some variables after that as bellow:

print (self.connected)
print (self.transport.connector.state)
print (self.transport.connected)
print (self.transport.reactor.running)
print (self.transport.socket._closed)
print (self.factory.protocol.connected)
print (self._writeDissconnected)

But any variables value didn't change after disconnecting my network :(

My question is: which variables will be set when the connection lost? I mean how can i check the connection status and if it's disconnect, how can i reconnect that?

Aida.Mirabadi
  • 996
  • 4
  • 10
  • 27
  • 1
    What's the while loop doing inside `connectionMade`? Does it prevent `connectionMade` from ever returning? – keturn Sep 02 '15 at 15:36
  • @keturn Thanks for your attention, The loop inside `connectionMade` is for checking or pinging the connection and notify when the connection lost. This loop doesn't prevent from returning. but the values that i printed in question, never changed after disconnecting from server. i need to know how can i check the connection status and if it's disconnect, how can i reconnect that? – Aida.Mirabadi Sep 03 '15 at 15:58

1 Answers1

1

override connectionLost method to catch disconnection. to official docs

Edit about reconnection: Reconnection mostly is a logical decision. You may want to add logic between 'connectionLost' and 'reconnect'.

Anyway, You can use ReconnectingClientFactory for better code. ps: using factory pattern at reconnection is best way to keep code clean and smart.

class MyEchoClient(Protocol):
    def dataReceived(self, data):
        someFuncProcessingData(data)
        self.transport.write(b'test')

class MyEchoClientFactory(ReconnectingClientFactory):
    def buildProtocol(self, addr):
        print 'Connected.'
        return MyEchoClient()

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection.  Reason:', reason
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason
        ReconnectingClientFactory.clientConnectionFailed(self, connector,
                                                     reason)
Aida.Mirabadi
  • 996
  • 4
  • 10
  • 27
cengizkrbck
  • 704
  • 6
  • 21
  • Thanks for your response. you mean i don't need to check connection in interval loop?? And you mean if the connection lost, the `connectionLost` method will be call automatically ? if it's true, how i can reconnect that? it's better if you give me sample code or reference :) – Aida.Mirabadi Sep 03 '15 at 16:09
  • Yes. exactly.Yo will forgot to use loops with twisted :D – cengizkrbck Sep 04 '15 at 07:10