gpt4 book ai didi

python - 线程 WebSocket 服务器中的 Ping 和 Pong (Python)

转载 作者:行者123 更新时间:2023-11-30 23:47:16 25 4
gpt4 key购买 nike

我用 Python 编写了一个线程化的 websocket 服务器,使用 lastest websocket spec我试图让它每 x 秒向每个客户端发送一次 ping 请求。我想出的唯一方法是覆盖 BaseServer.server_forever()像这样:

# override BaseServer's serve_forever to send a ping request every now and then
class ModTCPServer(SocketServer.TCPServer):
def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate)
self.__is_shut_down = threading.Event()
self.__shutdown_request = False

def serve_forever(self, poll_interval=0.5):
###
global PING_EVERY_SEC
self.lastPing = int(time())
###
self.__is_shut_down.clear()
try:
while not self.__shutdown_request:
r, w, e = select.select([self], [], [], poll_interval)
if self in r:
self._handle_request_noblock()
###
now = int(time())
if (now - self.lastPing) >= PING_EVERY_SEC:
self.socket.send(WebSocketPing(['0x89','0x21','0xa7','0x4b'], now)) # arbitrary key
self.lastPing = now
###
finally:
self.__shutdown_request = False
self.__is_shut_down.set()

class LoginServer(SocketServer.ThreadingMixIn, ModTCPServer):
pass

server = LoginServer(("", PORT), ApplicationHandler)
print "serving at port", PORT

server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()

while server_thread.isAlive():
pass

server.shutdown()

这是构造 Ping 帧的函数,它只是将时间戳放入内容中:

def WebSocketPing(key, timestamp=False):
data = ['0x89','0x8a'] # 0x89 = fin,ping 0x8a = masked,len=10
data.extend(key)
if timestamp:
t = str(timestamp)
else:
t = str(int(time()))
for i in range(10):
masking_byte = int(key[i%4],16)
masked = ord(t[i])
data.append(hex(masked ^ masking_byte))
frame = ''
for i in range(len(data)):
frame += chr(int(data[i],16))
return frame

当我运行这个时会发生不好的事情

Traceback (most recent call last):
File "LoginServer.py", line 91, in <module>
server = LoginServer(("", PORT), ApplicationHandler)
File "LoginServer.py", line 63, in __init__
SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate)
File "/usr/lib/python2.6/SocketServer.py", line 400, in __init__
self.server_bind()
File "/usr/lib/python2.6/SocketServer.py", line 411, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
socket.error: [Errno 112] Address already in use

我认为这是由于我对 Python 中重写的工作原理缺乏了解,或者是因为我对这个问题的处理方法根本错误。有没有更好的方法来做到这一点或让这段代码工作?

最佳答案

代码不会在任何地方设置属性 __is_shut_down__shutdown_request。因此,尝试访问它们会失败。在构造函数中创建它们,如下所示:

class ModTCPServer(SocketServer.TCPServer):
def __init__(self, *args, **kwargs):
SocketServer.TCPServer.__init__(self, *args, **kwargs)
self.__is_shut_down = threading.Event()
self.__shutdown_request = threading.Event()

响应更新:

socket.error: [Errno 112] Address already in use

表示另一个进程已经绑定(bind)到指定端口。在 Unix 上,您可以使用 sudo netstat -ltpn 找到该进程。或者,选择不同的端口。

关于python - 线程 WebSocket 服务器中的 Ping 和 Pong (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8434858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com