Hello all,
<br>
<br>I&#39;ve been trying the most basic (only a few lines) server/client
code example in the Twisted docs and can&#39;t get even that to work
(code included below). It&#39;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 &quot;Started to Connect&quot; and hangs, then times out with an error.
I&#39;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&#39;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);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def connectionMade(self):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.transport.write(&quot;An apple a day keeps the doctor away\r\n&quot;) 
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def dataReceived(self, data):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def startedConnecting(self, connector):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &#39;Started to connect.&#39;
</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);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def buildProtocol(self, addr):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &#39;Connected.&#39;
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def clientConnectionLost(self, connector, reason):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &#39;Lost connection.  Reason:&#39;, 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);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def clientConnectionFailed(self, connector, reason):
</span><br style="background-color: rgb(255, 204, 153);"><span style="background-color: rgb(255, 204, 153);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &#39;Connection failed. Reason:&#39;, 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(&#39;localhost&#39;, 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 &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
<br>&gt;&gt;&gt; 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 &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
<br>&gt;&gt;&gt; import EchoClient
<br>Started to connect.
<br>Connection failed. Reason: [Failure instance: Traceback (failure with no frames)
<br>: &lt;class &#39;twisted.internet.error.TimeoutError&#39;&gt;: User timeout caused connection
<br>failure.
<br>][/b]