[Twisted-Python] Help with TCPServer and listenTCP

James Healey healeyjames at yahoo.co.uk
Fri Oct 20 11:27:38 EDT 2006


This is my server code.......

from twisted.internet import reactor, protocol

class Echo(protocol.Protocol):
	"""This is just about the simplest possible
protocol"""
	
	def dataReceived(self, data):	
		if data == 1:
			print data
		
	def connectionMade(self):
		print "Client Connected to server"

def main():
    """This runs the protocol on port 8000"""
    factory = protocol.ServerFactory()
    factory.protocol = Echo
    reactor.listenTCP(8000,factory)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()


And here is my client code.....

from twisted.internet import reactor, protocol

class EchoClient(protocol.Protocol):
	"""Once connected, send a message, then print the
result."""
	
	def connectionMade(self):
		buildInfo = 1
		self.transport.write(buildInfo)
				
	def dataReceived(self, data):
		"As soon as any data is received, write it back."
		print "Server said:", data
		self.transport.loseConnection()
		
	def connectionLost(self, reason):
		print "connection lost"

class EchoFactory(protocol.ClientFactory):
	protocol = EchoClient
	build = 1
	
	def clientConnectionFailed(self, connector, reason):
		print "Connection failed - goodbye!"
		reactor.stop()
		
	def clientConnectionLost(self, connector, reason):
		print "Connection lost - goodbye!"
		reactor.stop()


# this connects the protocol to a server runing on
port 8000
def main():
    f = EchoFactory()
    reactor.connectTCP("localhost", 8000, f)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()


In this test im trying to send some data when the
client makes it's connection, but I get and error when
running the client.

>From all the examples i can find using write method
are all echo/chat programs and from that I guess only
strings/text can be send via the write method.


> Are you familiar with buildbot?

Heard of but never looked at.

I'm very new to this Twisted, and quite new to python.

Send instant messages to your online friends http://uk.messenger.yahoo.com 




More information about the Twisted-Python mailing list