尝试连接 119.23.124.81:7575
服务器每5秒会返回一个{"type":"ping"}
,我尝试用以下代码去连接,但是无法获取到这个{"type":"ping"}
:
s = socket(AF_INET, SOCK_STREAM)
# 建立连接:
s.connect(('119.23.124.81', 7575))
while True:
print(s.recv(1024).decode('utf-8'))
s.close()
代码不会报错,但是也获取到我想要的内容
请问要如何写才能获取到这个{"type":"ping"}
I figured it out, it turns out this is the websocket protocol used, not an ordinary socket
Just use the websocket library instead. The code is as follows:
Refer to official documentation
and
Because the data you send is a dictionary object, when sending data through the socket, use the pickle or json module to serialize the data before sending it. Correspondingly, the receiving end must use pickle or json for deserialization.