[Twisted-Python] cannot get simple example to run
klok000 at gmail.com
klok000 at gmail.com
Mon Apr 28 13:23:47 MDT 2008
Hello everyone. Try as I might I cannot get a very simple example to run.
I basically want a server that accepts connections, and periodically issues
messages to all clients. The docs for twisted.word says things are in flux
so I would rather avoid that and just use a LineReceiver. However right now
I can't get anything to work.
I start the server and it seems to be happy to sit around and accept
connections. However when I run the client it quits (without saying why)
and no connection is made. Any help would be very much appreciated. Here's
the code, log messages below that. Thanks!
Kevin
##################################################
# server.py
##################################################
import sys
from twisted.internet.protocol import log
from twisted.internet.protocol import Factory
from twisted.internet.protocol import Protocol
from twisted.internet import reactor
class Echo(Protocol):
def connectionMade(self):
sys.stderr.write('Server connectionMade\n')
self.factory.numProtocols = self.factory.numProtocols+1
if self.factory.numProtocols > 100:
self.transport.write("Too many connections, try later")
self.transport.loseConnection()
def connectionLost(self, reason):
sys.stderr.write('Server connectionLost\n')
self.factory.numProtocols = self.factory.numProtocols-1
def dataReceived(self, data):
sys.stderr.write('Server dataReceived\n')
self.transport.write(data)
factory = Factory()
factory.protocol = Echo
log.startLogging(file('server.log', 'w'))
sys.stderr.write('Server starting...\n')
# 8007 is the port you want to run under. Choose something >1024
reactor.listenTCP(9010, factory)
reactor.run()
sys.stderr.write('Server done\n')
##################################################
# client.py
##################################################
import sys
from twisted.internet import reactor
from twisted.internet.protocol import log
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import ClientCreator
from twisted.internet.protocol import ClientFactory
class Greeter(Protocol):
def sendMessage(self, msg):
sys.stderr.write('Client sendMessage\n')
self.transport.write("MESSAGE %s\n" % msg)
class GreeterClientFactory(ClientFactory):
def startedConnecting(self, connector):
sys.stderr.write('Client startedConnecting\n')
def buildProtocol(self, addr):
sys.stderr.write('Client connected\n')
g = Greeter()
reactor.callLater(1, g.sendMessage, "This is sent in 1 second")
reactor.callLater(2, g.sendMessage, "This is sent in 2 second")
reactor.callLater(3, g.sendMessage, "This is sent in 3 second")
return Greeter()
def clientConnectionLost(self, connector, reason):
sys.stderr.write('Client connectionLost: ' + reason + '\n')
def clientConnectionFailed(self, connector, reason):
sys.stderr.write('Client connectionFailed: ' + reason + '\n')
log.startLogging(file('client.log', 'w'))
sys.stderr.write('Client connecting...\n')
f = GreeterClientFactory()
reactor.connectTCP("localhost", 9010, f)
sys.stderr.write('Client done\n')
##################################################
# client.log
##################################################
2008/04/28 12:38 -0400 [-] Log opened.
2008/04/28 12:38 -0400 [-] Client connecting...
2008/04/28 12:38 -0400 [-] Starting factory <__main__.GreeterClientFactory
instance at 0x8354d8c>
2008/04/28 12:38 -0400 [-] Client startedConnecting
2008/04/28 12:38 -0400 [-] Client done
##################################################
# server.log
##################################################
2008/04/28 14:41 -0400 [-] Log opened.
2008/04/28 14:41 -0400 [-] Server starting...
2008/04/28 14:41 -0400 [-] twisted.internet.protocol.Factory starting on
9010
2008/04/28 14:41 -0400 [-] Starting factory
<twisted.internet.protocol.Factory instance at 0xb7cc18cc>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20080428/8a8aa965/attachment.html>
More information about the Twisted-Python
mailing list