我正在尝试连接到Websockets服务器以发送消息,但是由于无法建立会话,所以无法连接。我已经使用请求模块登录到网站,现在正在使用websockets模块进行连接。我试图提供以下代码中所示的正确标头,包括我在Chrome浏览器中发出的请求之一中扫描的先前存在的Sec-WebSocket-Key。它仍然不验证会话,然后使我与websockets服务器断开连接。如何成功连接到websockets服务器以发送命令?
我的Python代码:
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:65.0) Gecko/20100101 Firefox/65.0",
"Accept-Encoding": "gzip, deflate, br", "Referer": "https://bigboibets.com/",
"Cookie": "PHPSESSID=" + Client.PHPSESSID + "; hasVisited=true", "TE": "Trailers"}
Response = Client.Session.post(Client.Endpoints.WSAuthLink, headers=headers)
WSAuthLink = str(Response.text)
Response = Client.Session.get(WSAuthLink)
WSSessionKey = WSAuthLink.split("SessionKey=")[1]
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:65.0) Gecko/20100101 Firefox/65.0",
"Accept-Encoding": "gzip, deflate, br",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Pragma": "no-cache",
"Sec-WebSocket-Key": "9mq6jY7920lb9KF3bBb8vQ==",
"Sec-WebSocket-Extentions": "permessage-deflate",
"Upgrade": "websocket",
"Sec-WebSocket-Version": "13",
"Host": "pm3.bigboibets.com:2083",
"Origin": "https://pm3.bigboibets.com:2053"}
ws = create_connection("wss://pm3.bigboibets.com:2083/", header=headers)
ws.send(json.dumps({"Response":"Session","PC":"CA15818A","Version":"6.04","Language":"0","SitePassword":"root","ID":"","PNum":1}))
result = ws.recv()
print (result)
ws.send(json.dumps({"Response":"LoginRequest","Player":"USERNAME","ID":"0000049A","PNum":2}))
result = ws.recv()
print (result)
ws.close()
输出:
{"Command":"Message","Text":"Session expired. Refresh your browser page and login again.","Disconnect":"Yes"}
Traceback (most recent call last):
File "C:\Users\Brandon\Desktop\BBB Bot.py", line 123, in <module>
LoginAccount("USERNAME", "PASSWORD")
File "C:\Users\Brandon\Desktop\BBB Bot.py", line 43, in LoginAccount
RetrieveAccountData()
File "C:\Users\Brandon\Desktop\BBB Bot.py", line 67, in RetrieveAccountData
AuthWSSession(WSSessionID)
File "C:\Users\Brandon\Desktop\BBB Bot.py", line 110, in AuthWSSession
result = ws.recv()
File "C:\Users\Brandon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websocket\_core.py", line 313, in recv
opcode, data = self.recv_data()
File "C:\Users\Brandon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websocket\_core.py", line 330, in recv_data
opcode, frame = self.recv_data_frame(control_frame)
File "C:\Users\Brandon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websocket\_core.py", line 343, in recv_data_frame
frame = self.recv_frame()
File "C:\Users\Brandon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websocket\_core.py", line 377, in recv_frame
return self.frame_buffer.recv_frame()
File "C:\Users\Brandon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websocket\_abnf.py", line 361, in recv_frame
self.recv_header()
File "C:\Users\Brandon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websocket\_abnf.py", line 309, in recv_header
header = self.recv_strict(2)
File "C:\Users\Brandon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websocket\_abnf.py", line 396, in recv_strict
bytes_ = self.recv(min(16384, shortage))
File "C:\Users\Brandon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websocket\_core.py", line 452, in _recv
return recv(self.sock, bufsize)
File "C:\Users\Brandon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websocket\_socket.py", line 112, in recv
"Connection is already closed.")
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.
输出显示,在发出1个请求后,将发送会话过期的响应,然后进行第二次尝试,但连接已从端点关闭。 这意味着尽管我正在发送以前使用过的Sec-WebSocket-Key和其他甚至不需要的其他标头,但我发送的标头是不正确的。
我要连接的端点是:wss://pm3.bigboibets.com:2083 /
Screenshot of the request headers I found
如何使它连接到websockets服务器并在chrome浏览器中发出类似请求?