[Python] Python bypass cloudflare, connecting to websocket

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I need to connect to a websocket in Python, but the problem is that the server where this websocket is located has Cloudflare protection, which blocks the connection to it and returns a 403 error. Try doing this:

    import asyncio
    import websockets

    async def connect_ws():
    cookies = {
    "token": "...",
    "lb-session-id": "...",
    }
    headers = {
    "accept-encoding": "gzip, deflate, br, zstd",
    "accept-language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
    "connection": "Upgrade",
    "cookies": "; ".join([f"{k}={v}" for k, v in cookies.items()]),
    "host": "playerok.com",
    "origin": "https://playerok.com",
    "sec-websocket-protocol": "graphql-transport-ws",
    "sec-websocket-version": "13",
    "upgrade": "websocket",
    "user-agent": "..."
    }

    try:
    async with websockets.connect(
    "wss://playerok.com/subscriptions",
    extra_headers=headers
    ) as ws:
    await ws.send('{"type":"connection_init"}')
    print(await ws.recv())
    except websockets.exceptions.InvalidStatusCode as e:
    print(e.status_code)
    print(e.headers)

    asyncio.run(connect_ws())


    I get error 403. Please help!

    Continue reading...

Compartilhe esta Página