1

I'm new in twisted and in web programming itself. What I want is to implement server and client (and pass some string data between them). The problem is, that the data is important, so I want client to resend it to server in case of loosing connection or in case it wasn't full on the server side. But I don't want to resend it in case it was fully received, so I don't think that just adding the logic to def connectionLost() will do. How could this be done?

This is my server (just the same as in doc examples) and (after ------------) is the client:

from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor

class ConsoleReceiver(Protocol):

    def connectionMade(self):
        self.transport.write(
            "Welcome!\n".encode('utf-8'))

    def dataReceived(self, data):
        self.transport.write('Data received, thanks'.encode('utf-8'))
        data = data.decode('ascii')
        print(data)
        self.transport.loseConnection()

class ServerFactory(Factory):

    def buildProtocol(self, addr):
        return ConsoleReceiver()

if __name__ == '__main__':
    endpoint = TCP4ServerEndpoint(reactor, 21285)
    endpoint.listen(ServerFactory())
    reactor.run()``` 

-----------------------------------------------------------------------------


@some_flask_route.route('/test/')

    urgent_information = <getting some urgent information from db with flask_sqlalchemy>

    reactor.connectTCP('localhost', 21285, ShiftInfoSenderFactory(urgent_information))
    reactor.run()


class ShiftInfoSender(Protocol):
    def __init__(self, urgent_information):
        self.urgent_information = urgent_information

    def connectionMade(self):
        self.transport.write('\nInformation to be red:{}\n'.format(self.urgent_information[1]).encode('utf-8'))
        for i in self.urgent_information[2]:
            self.transport.write('Some unpacked information: {}'.format(i).encode('utf-8')

    def dataReceived(self, data):
        print(data.decode('ascii'))


class ShiftInfoSenderFactory(ClientFactory):
    def __init__(self, urgent_information):
        self.urgent_information = urgent_information

    def startedConnecting(self, connector):
        print('Started to connect')

    def buildProtocol(self, addr):
        print('Connected')
        return ShiftInfoSender(self.urgent_information)

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

    def clientConnectionFailed(self, connector, reason):
        print('Connection failed. Reason:', reason) ``` 




  • Put length information before your data and then check if all the data according to the length were received. – Steffen Ullrich Dec 23 '19 at 17:02
  • Does this answer your question? [How does the python socket.recv() method know that the end of the message has been reached?](https://stackoverflow.com/questions/41382127/how-does-the-python-socket-recv-method-know-that-the-end-of-the-message-has-be) – Steffen Ullrich Dec 23 '19 at 17:03
  • @SteffenUllrich Am I right that there is no desigion of it in twisted from the box, and I just have to add the check and resending logic to my protocols by hand? Besides, thanks for the advise to send and check the length! I'll try to add this logic. – Ekatherine Markus Dec 24 '19 at 08:38
  • If you just use plain TCP with twisted then it will just use plain TCP which has no idea of messages. Twisted supports higher level abstractions which help with this (like [IntNStringReceiver](https://stackoverflow.com/questions/28306076/twisted-int16stringreceiver-little-endian-byte-order)) but you have to use these. – Steffen Ullrich Dec 24 '19 at 10:08

0 Answers0