lexiforest / curl_cffi

Python binding for curl-impersonate via cffi. A http client that can impersonate browser tls/ja3/http2 fingerprints.
https://curl-cffi.readthedocs.io/
MIT License
1.93k stars 240 forks source link

[BUG] Websocket not connecting #241

Closed STRATZ-Ken closed 3 months ago

STRATZ-Ken commented 7 months ago

Describe the bug Getting the following error : Failed to WS_RECV, ErrCode: 52, Reason: ''. This may be a libcurl error, See https://curl.se/libcurl/c/libcurl-errors.html first for more details.

To Reproduce

from curl_cffi.requests import Session, WebSocket
import json

msg_count = 0

def on_message(ws: WebSocket, message):
    global msg_count

    print("------------------------------------------------------")
    print(message)
    print("======================================================")

    msg_count += 1
    if msg_count >= 100:
        ws.close()

def on_error(ws: WebSocket, error):
    print(error)

def on_open(ws: WebSocket):
    print("For websockets, you need to set $wss_proxy environment variable!\n"
          "$https_proxy will not work!")
    print(">>> Websocket open!")

def on_close(ws: WebSocket):
    print("<<< Websocket closed!")

with Session() as s:
    ws = s.ws_connect(
        "wss://api.hardrocksportsbook.com/graphql-ws",
        on_open=on_open,
        on_close=on_close,
        on_message=on_message,
        on_error=on_error,
    )
    message = {"SportsbookLoginRequest": {"application": "sportsbook", "channel": "ARIZONA_ONLINE", "locale": "enus-us-x-az"}}
    tosend = json.dumps(message).encode()
    ws.send(tosend)
    ws.run_forever()

Expected behavior The response should be {"Response":{"status":"ok","reqId":"0"}}

Versions

Additional context

perklet commented 7 months ago

Sorry, I can not reproduce this error, I'm getting:

ErrCode: 22, Reason: 'Refused WebSockets upgrade: 403'