如何在Python中使用gevent.server设置连接之间的超时?
from gevent.server import StreamServer
def handle(socket, address):
message = socket.recv(1024)
if __name__ == "__main__":
server = StreamServer((SERVER_HOST, SERVER_PORT), handle)
server.serve_forever()
我想在连接之间建立延迟,例如1秒。 只在来自一个ip的连接之间建立延迟是很好的。
谢谢。
答案 0 :(得分:0)
from gevent.server import StreamServer
import gevent
lock = {}
def handle(socket, address):
if address[0] in lock.keys():
print "sleep"
gevent.sleep(1)
handle(socket, address)
lock[address[0]] = 1
try:
message = socket.recv(1024)
print address
finally:
del lock[address[0]]
if __name__ == "__main__":
server = StreamServer(('0.0.0.0', 9999), handle)
server.serve_forever()