[Twisted-Python] Please help: having problems with the simplest Twisted Server/Client Demo Example from the Tutorial
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
import EchoClient Started to connect. Connection failed. Reason: [Failure instance: Traceback (failure with no
..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. frames) : <class 'twisted.internet.error.TimeoutError'>: User timeout caused connection failure. ][/b]
1. Certain Python prompts (e.g. idle) may not work well with Twisted. Better to run the program directly: python.exe myscript.py 2. Might want to start a log: from twisted.python import log log.startLogging(sys.stderr) 3. Might want to flush when writing to stdout(). At first glance, however, your code looks fine.
Thanks Itamar, That was exactly the problem. The Twisted server/client example runs fine when the program is run directly (> python.exe mycode.py) but not when imported at the python prompt. Best, Sajit On Dec 8, 2007 8:27 PM, Itamar Shtull-Trauring <itamar@itamarst.org> wrote:
1. Certain Python prompts (e.g. idle) may not work well with Twisted. Better to run the program directly:
python.exe myscript.py
2. Might want to start a log:
from twisted.python import log log.startLogging(sys.stderr)
3. Might want to flush when writing to stdout().
At first glance, however, your code looks fine.
On 01:10 am, sajitrao@gmail.com wrote:
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.
Your code, for what it's worth, looks correct. Try running it on a different machine. (A stock Ubuntu image in VMWare would be best.) The most common cause of this problem is a software firewall popping up a dialog box underneath the window that you're looking at, or denying traffic to 'python.exe' altogether. Another cause I've seen once or twice is the name "localhost" being misconfigured. Try specifically connecting to "127.0.0.1" instead.
participants (3)
-
glyph@divmod.com -
Itamar Shtull-Trauring -
Sajit Rao