Hello all,
<br>
<br>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.
<br>
<br>The problem is that the client <span style="font-weight: bold;">never connects with the server</span>.
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. <br>
<br><span style="font-weight: bold;">Server Code:</span>
<br>
<br><span style="background-color: rgb(255, 204, 153);">from twisted.internet.protocol import Factory, Protocol
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">from twisted.internet import reactor
</span><br style="background-color: rgb(255, 153, 102);">
<br style="background-color: rgb(255, 153, 102);"><span style="background-color: rgb(255, 204, 153);">class QOTD(Protocol):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> def connectionMade(self):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> self.transport.write("An apple a day keeps the doctor away\r\n")
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> self.transport.loseConnection()</span><br style="background-color: rgb(255, 204, 153);">
<br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">factory = Factory()
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">factory.protocol = QOTD
</span><br style="background-color: rgb(255, 204, 153);">
<br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">reactor.listenTCP(8007, factory)
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">reactor.run()
</span><br style="background-color: rgb(255, 153, 102);">
<br><span style="font-weight: bold;">Client Code:</span>
<br>
<br><span style="background-color: rgb(255, 204, 153);">from twisted.internet.protocol import Protocol, ClientFactory
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">from sys import stdout
</span><br style="background-color: rgb(255, 204, 153);">
<br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">class Echo(Protocol):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> def dataReceived(self, data):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> stdout.write(data)
</span><br style="background-color: rgb(255, 204, 153);">
<br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">class EchoClientFactory(ClientFactory):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> def startedConnecting(self, connector):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> print 'Started to connect.'
</span><br style="background-color: rgb(255, 204, 153);">
<br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> def buildProtocol(self, addr):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> print 'Connected.'
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> return Echo()
</span><br style="background-color: rgb(255, 204, 153);">
<br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> def clientConnectionLost(self, connector, reason):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> print 'Lost connection. Reason:', reason
</span><br style="background-color: rgb(255, 204, 153);">
<br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> def clientConnectionFailed(self, connector, reason):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);"> print 'Connection failed. Reason:', reason
</span><br style="background-color: rgb(255, 204, 153);">
<br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">from twisted.internet import reactor
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">reactor.connectTCP('localhost', 8007, EchoClientFactory())
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">reactor.run()
</span><br style="background-color: rgb(255, 204, 153);">
<br><span style="font-weight: bold;"><br>Result</span>
<br>
<br>Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on
<br>win32
<br>Type "help", "copyright", "credits" or "license" for more information.
<br>>>> import QOTDServer
<br>
<br>..then I start the client in a separate window...
<br>
<br>Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on
<br>win32
<br>Type "help", "copyright", "credits" or "license" for more information.
<br>>>> import EchoClient
<br>Started to connect.
<br>Connection failed. Reason: [Failure instance: Traceback (failure with no frames)
<br>: <class 'twisted.internet.error.TimeoutError'>: User timeout caused connection
<br>failure.
<br>][/b]