Python - Websockets 无法验证我的会话

2024-09-27 00:17:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图连接到websockets服务器以发送消息,但是由于它没有建立会话,我无法连接。我已经使用请求模块登录到网站,现在正在使用websockets模块来连接。我已经尝试提供正确的头,如下面的代码所示,包括一个以前存在的Sec WebSocket密钥,我在Chrome浏览器中的一个请求中扫描了这个密钥。它仍然不验证会话,然后断开我与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()

输出:

^{pr2}$

输出显示,在1个请求之后,发送session expired的响应,然后进行第二次尝试,但连接已从端点关闭。 这意味着我发送的头是不正确的,尽管我正在发送一个以前使用过的Sec-WebSocket密钥和其他很可能根本不需要的头。在

我尝试连接的端点是:wss://pm3.bigboibets.com:2083/

Screenshot of the request headers I found

如何让它连接到websockets服务器并在chrome浏览器中发出这样的请求?在

Logs of websocket frames in chrome browser


Tags: 服务器comclientwswebsocketsresponse密钥sec

热门问题