python -- twisted初探

Twisted是用python实现的基于事件驱动的网络引擎框架。

初步使用twisted

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 from twisted.internet import protocol
 5 from twisted.internet import reactor
 6 
 7 class Echo(protocol.Protocol):
 8     def dataReceived(self, data):
 9         self.transport.write(bytes("I am server | %s" %data.decode('utf8'),'utf8'))
10 
11 def main():
12     factory = protocol.ServerFactory()
13     factory.protocol = Echo
14     reactor.listenTCP(9999,factory,interface='localhost')
15     reactor.run()
16 
17 if __name__ == '__main__':
18     print("