简体   繁体   中英

Calling the sendLine() method after the connectTCP method has been called on twisted

I made a simple server that uses the LineReceiver. I can manage to send and receive messages when I test with Telnet.

I want to take things a little bit further and use a small GUI to send and receive the messages.

The way I am picturing this is that I somehow will have some sort of connect function that will call the reactor.run() method, and also make an instance of the ClientFactory class. I am basing myself with this example I found:

#!/usr/bin/env python

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.


from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
import sys

class EchoClient(LineReceiver):
    end="Bye-bye!"
    def connectionMade(self):
        self.sendLine("Hello, world!")
        self.sendLine("What a fine day it is.")
        self.sendLine(self.end)

    def lineReceived(self, line):
        print "receive:", line
        if line==self.end:
            self.transport.loseConnection()

    def sendMessage(self, line):
        self.sendLine(line)

class EchoClientFactory(ClientFactory):
    protocol = EchoClient

    def clientConnectionFailed(self, connector, reason):
        print 'connection failed:', reason.getErrorMessage()
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print 'connection lost:', reason.getErrorMessage()
        reactor.stop()

def main():
    factory = EchoClientFactory()
    reactor.connectTCP('localhost', 1234, factory)
    reactor.run()

if __name__ == '__main__':
    main()

Observe that when I call the main() function, the connectionMade will send those messages.

How can I run the reactor and the factory, and call the sendLine function?

Twisted calls are broken up into things that queue future actions and things that act immediately (or near to immediately) if the reactor is running. You can combine those to schedule something to happen in the future. (see: Scheduling tasks for the future )

So if you want to call sendLine in the future you could use reactor.callLater(5, sendLine, arg_to_sendLine) . That would schedule the sendLine call to be run 5 seconds after the callLater call (... Assuming that your code is in the reactor.run() state)

You also said:

The way I am picturing this is that I somehow will have some sort of connect function that will call the reactor.run() method

That statement worries me because Twisted is an all-encompassing framework (in most twisted programs reactor.run() and it's setup calls become the whole of main ) not just something that you start when you want to do communications (it reacts poorly when you try and only use it halfway).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM