【发布时间】:2015-08-20 02:44:18
【问题描述】:
我正在使用twisted 编写一个简单的Web 服务器应用程序。应用程序将获取一个字符串并返回它接收到的字符串的反转。
一切正常。现在如果有 5 分钟不活动,我需要关闭套接字连接。
这是我的服务器代码:-
from twisted.internet import reactor, protocol
class Echo(protocol.Protocol):
"""This is just about the simplest possible protocol"""
def dataReceived(self, data):
"As soon as any data is received, write it back."
self.transport.write(data[::-1])
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()
~
【问题讨论】: