Skip to content Skip to sidebar Skip to footer

I Have A Twisted Reactor Running, How Do I Connect To It?

I've been following the tutorials and now have a twisted reactor running. I've used telnet to test that it does stuff but I've not managed to find anything in the twisted tutorials

Solution 1:

Here's a small example of a client capable of connecting to your server above.

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

classTTTClientProtocol(LineReceiver):
    deflineReceived(self, line):
        line = line.strip()
        if line == 'You are connected':
            self.sendLine(self.factory.username)
        else:
            print'SERVER SAYS:', line

classTTTClientFactory(ClientFactory):
    protocol = TTTClientProtocol 

    def__init__(self, name):
        self.username = name


name = raw_input('Please enter your name: ')
print'Connecting...'

reactor.connectTCP('localhost', 8007, TTTClientFactory(name))

reactor.run()

I kept it as simple as I could so you could understand it easily, but to implement the chat part I would need code to read from stdin without blocking the reactor. Since you mentioned you're using a GUI instead of terminal standard input/output, then that is a lot easier actually - just choose a reactor compatible with your GUI library and then use your normal GUI events.

Hope that helps...

Post a Comment for "I Have A Twisted Reactor Running, How Do I Connect To It?"