pythonsslwebsocketautobahn

Python websocket create connection


I have this server

https://github.com/crossbario/autobahn-python/blob/master/examples/twisted/websocket/echo_tls/server.py

And I want to connect to the server with this code:

ws = create_connection("wss://127.0.0.1:9000")

What options do I need to add to create_connection? Adding sslopt={"cert_reqs": ssl.CERT_NONE} does not work:

websocket._exceptions.WebSocketBadStatusException: Handshake status 400

Solution

  • This works

    import asyncio
    import websockets
    import ssl
    
    async def hello():
        async with websockets.connect('wss://127.0.0.1:9000',ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLS)) as websocket:
            data = 'hi'
            await websocket.send(data)
            print("> {}".format(data))
    
            response = await websocket.recv()
            print("< {}".format(response))
    
    asyncio.get_event_loop().run_until_complete(hello())