python---强大的twisted 网络编程框架(简单应用)

强大的twisted 网络编程框架

服务器端

from twisted.internet import reactor
from twisted.internet.protocol import Protocol, Factory

class SimpleLogger(Protocol):
	def connectionMade (self):
		print 'Got connection from ', self.transport.client

	def connectionLost (self,reason):
		print self.transport.client, 'disconnected'

	def dataReceived (self,data):
		print data

factory = Factory()
factory.protocol = SimpleLogger

reactor.listenTCP(1234,factory)
reactor.run()

客户端

import socket
s = socket.socket()
host = socket.gethostname()
port = 1234

s.connect((host,port))
print s.recv(1024)

详细参考资料

http://twistedmatrix.com/trac/

你可能感兴趣的:(python---强大的twisted 网络编程框架(简单应用))