pythonwebsocket

Python websocket library, how to print handshake?


I am using the Python websocket library. I need to see the handshake payload.

My code effectively looks like this:

from websocket import create_connection

ws = create_connection("the websocket url")

I found:

ws.enableTrace(True)

Unfortunately I cannot put it before the connection/object is created and afterwards is too late.

I tried adding this as well:

import logging

logging.basicConfig(
    format="%(asctime)s %(message)s",
    level=logging.DEBUG,
)

Unfortunately, it did not do anything.

Any solutions?


Solution

  • You can call websocket.enableTrace(True) directly on the class instead of the instance before assignment. Learn more here.

    Example:

    import websocket
    
    websocket.enableTrace(True)
    ws = websocket.WebSocket()
    ws.connect("ws://echo.websocket.events/", origin="testing_websockets.com")
    ws.send("Hello, Server")
    print(ws.recv())
    ws.close()