1

This is my code `

class TestProtocol(Protocol):
    def connectionMade(self):
        print 'Got connection from', self.transport.client

    def dataReceived(self, data):
            print(data)


class Test(Factory, HoneypotService):
    NAME = 'test'
    protocol = TestProtocol

    def __init__(self, config=None, logger=None):
        HoneypotService.__init__(self, config=config, logger=logger)
        self.port = 8888

    def getService(self):
        return internet.TCPServer(self.port, self)

When I use the telnet 127.0.0.1 8888 command to connect to this service, and input abcdef,Method dataReceived is executed twice,execute result is as follows:

output picture: enter image description here

Is there any way to execute dataReceived only once, And the output information is abcdef. I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this. 

The output information is abcdef.

ljs
  • 11
  • 2

1 Answers1

1

As explained in the Twisted FAQ:

TCP is a stream-oriented transport. This means that when you call transport.write, the data may be broken up into arbitrarily-sized chunks for transmission over the network. There is no way for Twisted to determine how large the data originally written to the transport was.

If you want to send a message and receive it whole on the other end of a connection, you must decide on a format for the message and parse it. For example, prefixing the message with a length or terminating it with a message boundary.

https://github.com/twisted/trac-wiki-archive/blob/trunk/FrequentlyAskedQuestions.mediawiki#why-is-protocoldatareceived-called-with-only-part-of-the-data-i-called-transportwrite-with

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
  • I see what you mean and read Twisted FAQ,But there's no example of how to use rawDataReceived。I change my code ‘’ def dataReceived(self, data): self._buffer += data if self._busyReceiving: return try: self._busyReceiving = True print "_buffer", self._buffer finally: self._busyReceiving = False ‘’ output result :The dataReceived method is executed twice – ljs Dec 27 '22 at 07:47
  • When I rewrite rawDataReceived, it doesn't trigger it – ljs Dec 27 '22 at 07:59
  • 1
    I can't read the code in that comment because of how it is formatted. I don't know why you are getting rawDataReceived involved though. – Jean-Paul Calderone Dec 27 '22 at 13:25
  • use raw mode of receiver,Received complete data 'abcedf'. – ljs Dec 28 '22 at 08:10
  • 1
    Nope. That's not what "raw" mode of *LineReceiver* (not *Protocol*) does. – Jean-Paul Calderone Dec 28 '22 at 14:17
  • How can I use "raw" mode,I just want Received complete data 'abcedf' – ljs Dec 29 '22 at 02:01
  • 1
    You're already in raw mode. That's why you don't receive the whole chunk of data that you want at once. You need a framing protocol so you can assemble sub-chunks into whole chunks. You didn't mention what you're trying to accomplish so I don't know what framing protocol you're using, or what would be appropriate to add to your application. – Jean-Paul Calderone Dec 29 '22 at 20:40