1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to fix websocket connection being rejected

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to connect to a websocket as you would if you were to visit this website normally on a web browser. I am trying to obtain the jobs data on this website. However, when I run my code, it gives me the error websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 400. I am unsure how to fix this error. Could someone please assist.

    import asyncio
    import websockets

    async def connect_to_websocket():
    uri = "wss://gen3-nsw-server-prod.service.signalr.net/client/?hub=componenthub&asrs.op=%2F_blazor&negotiateVersion=1&asrs_request_id=%2FwGCJwgAAAA%3D&id=rPvis-H4B8CM0yPfreKIuw-7BSWwp02&access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjE3Nzk3NzAwNzYifQ.eyJhc3JzLnMuc24iOiJ3bjFtZHdrMDAwNktMXzU4YTgyYmU3NGVkYTRiNzhhODQwMzFmMzJlNTAxMjhlIiwiYXNycy5zLnNzdGlja3kiOiJSZXF1aXJlZCIsImFzcnMucy5jaHQiOiIzMCIsIm5iZiI6MTcyNjQ0MjUyNiwiZXhwIjoxNzI2NDQ2MTI2LCJpYXQiOjE3MjY0NDI1MjYsImF1ZCI6Imh0dHBzOi8vZ2VuMy1uc3ctc2VydmVyLXByb2Quc2VydmljZS5zaWduYWxyLm5ldC9jbGllbnQvP2h1Yj1jb21wb25lbnRodWIifQ.PTSRVomhWVNffGUnQrzBWkar-1iR4cSO2Bn6TE33bZ8"
    async with websockets.connect(uri) as websocket:
    # Send a message to the WebSocket server
    message = '{"protocol":"blazorpack","version":1}\x1e'
    print(f"Sending message: {message}")
    await websocket.send(message)

    # Receive the echoed message from the WebSocket server
    response = await websocket.recv()
    print(f"Received echoed message: {response}")

    # Run the WebSocket connection asynchronously
    asyncio.get_event_loop().run_until_complete(connect_to_websocket())

    Continue reading...

Compartilhe esta Página