用twisted编写服务器、客户端,最简单的实现代码大致如下:
(以下摘自twisted's howto文档,以客户端为例,有一些更改。选择客户端作为例子,主要是因为在reactor架构中,客户端的处理比服务器要复杂一些)
from twisted.internet.protocol import Protocol
from sys import stdout
class Echo(Protocol):
def dataReceived(self, data):
stdout.write(data)
stdout.write('\n')
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ClientFactory
factory = ClientFactory()
factory.protocol = Echo
reactor.connectTCP('localhost', 9999, factory)
reactor.run()
from sys import stdout
class Echo(Protocol):
def dataReceived(self, data):
stdout.write(data)
stdout.write('\n')
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ClientFactory
factory = ClientFactory()
factory.protocol = Echo
reactor.connectTCP('localhost', 9999, factory)
reactor.run()
这段代码能够连接到一个打开的服务器端口,如果收到数据,则会输出到屏幕。
当然这是最简单的情况,通常我们还要完成一些其它的功能,比如连接状况等,我们对Echo类简单重写它的几个方法,就能完成这些功能。修改如下:
class Echo(Protocol):
def dataReceived(self, data):
stdout.write(data)
stdout.write('\n')
def connectionMade (self):
stdout.write('connection made\n')
def connectionLost (self, reason):
stdout.write('connection lost\n')
def dataReceived(self, data):
stdout.write(data)
stdout.write('\n')
def connectionMade (self):
stdout.write('connection made\n')
def connectionLost (self, reason):
stdout.write('connection lost\n')
reactor对键盘的Ctrl+C事件做了处理,可以保证按Ctrl+C时,安全地断开所有连接。
这里仅仅能输出连接成功以后的状况,原因也很简单——Echo做为逻辑上的一个“连接”,只在连接成功以后才有效(当然不是说它在连接以后才建立的)。
为了能够完整输出连接过程,我们要对ClientFactory做一些修改扩充(当然不是修改它的原代码,简单派生就可以了),建立连接也可以简单一点点,完整代码如下:
from twisted.internet.protocol import Protocol
from sys import stdout
class Echo(Protocol):
def dataReceived(self, data):
stdout.write(data)
stdout.write('\n')
def connectionMade (self):
stdout.write('connection made\n')
def connectionLost (self, reason):
stdout.write('connection lost\n')
from twisted.internet.protocol import ClientFactory
class EchoClientFactory(ClientFactory):
def buildProtocol (self, addr):
return Echo()
def clientConnectionLost(self, connector, reason):
stdout.write('connection lost\n')
def clientConnectionFailed(self, connector, reason):
stdout.write('connection failed\n')
def startedConnecting(self, connector):
stdout.write('started connecting\n')
from twisted.internet import reactor
factory = EchoClientFactory()
reactor.connectTCP('localhost', 9999, factory)
reactor.run()
from sys import stdout
class Echo(Protocol):
def dataReceived(self, data):
stdout.write(data)
stdout.write('\n')
def connectionMade (self):
stdout.write('connection made\n')
def connectionLost (self, reason):
stdout.write('connection lost\n')
from twisted.internet.protocol import ClientFactory
class EchoClientFactory(ClientFactory):
def buildProtocol (self, addr):
return Echo()
def clientConnectionLost(self, connector, reason):
stdout.write('connection lost\n')
def clientConnectionFailed(self, connector, reason):
stdout.write('connection failed\n')
def startedConnecting(self, connector):
stdout.write('started connecting\n')
from twisted.internet import reactor
factory = EchoClientFactory()
reactor.connectTCP('localhost', 9999, factory)
reactor.run()