1 year ago

#209890

test-img

mohsen Salari

set time for check connection tcp client PYTHON:TWISTED

Im creating an application to receive data from multiple iot devices Each iot device has a socket server so i need to create multi socket client Im using Twisted library for create multiple socket client Problem is that my application can't detect connection failure and it wont try for reconnection

Herer is my code:

import time
import requests
from twisted.internet.protocol import Protocol, ReconnectingClientFactory as rcf, connectionDone
from twisted.internet import reactor
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.python import failure

client_list = [("device ip", device_port)]
endpoints = {}


class Client(Protocol):
    """
    when receive a massage call this function
    """

    def connectionMade(self):
        self.transport.setTcpKeepAlive(True)

    def dataReceived(self, data):
        reactor.callInThread(self._dataReceived, data)

    def _dataReceived(self, data):
        if self.connected == 1:
            data = data[:-1].decode("utf-8")
            data = data.split('\n')
            for tag in data:
                reactor.callInThread(self._push_data, tag, self.transport.addr[0])

    def _push_data(self, data, ip):
        print("start pushing")
        print(data[1:-1])
        try:
            pass
            // send a web request
        except Exception as xp:
            print(xp, end="\n")


class ClientFactory(rcf):
    def buildProtocol(self, addr):
        print(addr, 'Connected.')
        return Client()


if __name__ == "__main__":
    for client in client_list:
        endpoint = TCP4ClientEndpoint(reactor, client[0], client[1])
        endpoints[client] = endpoint
        endpoint.connect(ClientFactory())
    reactor.run()

i writing some code for fix this problem but that's not worked

import time
import requests
from twisted.internet.protocol import Protocol, ReconnectingClientFactory as clf, connectionDone
from twisted.internet import reactor
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.python import failure

client_list = [("device ip", device_port)]
endpoints = {}


class Client(Protocol):
    """
    when receive a massage call this function
    """

    def dataReceived(self, data):
        reactor.callInThread(self._dataReceived, data)

    def _dataReceived(self, data):
        if self.connected == 1:
            data = data[:-1].decode("utf-8")
            data = data.split('\n')
            for tag in data:
                reactor.callInThread(self._push_data, tag, self.transport.addr[0])

    def _push_data(self, data, ip):
        print("start pushing")
        print(data[1:-1])
        try:
            pass
            // send a web request
        except Exception as xp:
            print(xp, end="\n")

    def connectionLost(self, reason: failure.Failure = connectionDone):
        try:
            endpoints.pop(self.transport.addr[0])
        except Exception as xp:
            print(xp)
        print(f"Protocol client {self.transport.addr[0]} connectionLost \n ", reason)
        status = True
        while status:
            try:
                endpoint = TCP4ClientEndpoint(reactor, self.transport.addr[0], 100)
                endpoints[client] = endpoint
                endpoint.connect(ClientFactory())
                status = False
            except Exception as xp:
                print(xp)


class ClientFactory(clf):
    def buildProtocol(self, addr):
        print(addr, 'Connected.')
        return Client()

    def clientConnectionFailed(self, connector, reason):
        print("can't start connect to the server")
        print(reason)
        clf.clientConnectionFailed(self, connector, reason)

    def clientConnectionLost(self, connector, reason):
        print(reason)
        clf.clientConnectionLost(self, connector, reason)


if __name__ == "__main__":
    for client in client_list:
        endpoint = TCP4ClientEndpoint(reactor, client[0], client[1])
        endpoints[client] = endpoint
        endpoint.connect(ClientFactory())
    reactor.run()

my question is how i can set an interval ping for check connection

python

twisted

tcpsocket

twisted.internet

twisted.client

0 Answers

Your Answer

Accepted video resources