[Twisted-Python] Please help: having problems with the simplest Twisted Server/Client Demo Example from the Tutorial

Sajit Rao sajitrao at gmail.com
Sat Dec 8 20:10:25 EST 2007


Hello all,

I've been trying the most basic (only a few lines) server/client code
example in the Twisted docs and can't get even that to work (code included
below). It's very frustrating, because I did get a simple server/client
example working without using Twisted. Any help will be greatly appreciated.


The problem is that the client never connects with the server. it prints
"Started to Connect" and hangs, then times out with an error. I'm trying
this on my laptop running WinXP - I first start the server program (from the
command line python prompt) and then the client program in another window on
the same machine. Please let me know what I'm doing wrong.

Server Code:

from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor

class QOTD(Protocol):
         def connectionMade(self):
              self.transport.write("An apple a day keeps the doctor
away\r\n")
              self.transport.loseConnection()

factory = Factory()
factory.protocol = QOTD

reactor.listenTCP(8007, factory)
reactor.run()

Client Code:

from twisted.internet.protocol import Protocol, ClientFactory
from sys import stdout

class Echo(Protocol):
         def dataReceived(self, data):
              stdout.write(data)

class EchoClientFactory(ClientFactory):
         def startedConnecting(self, connector):
               print 'Started to connect.'

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

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

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

from twisted.internet import reactor
reactor.connectTCP('localhost', 8007, EchoClientFactory())
reactor.run()


Result

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)]
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import QOTDServer

..then I start the client in a separate window...

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)]
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import EchoClient
Started to connect.
Connection failed. Reason: [Failure instance: Traceback (failure with no
frames)
: <class 'twisted.internet.error.TimeoutError'>: User timeout caused
connection
failure.
][/b]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20071208/64a0ca7b/attachment.htm 


More information about the Twisted-Python mailing list