1

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
4
  • Are you getting any errors? Commented Aug 25, 2017 at 7:07
  • Yes, websocket._exceptions.WebSocketBadStatusException: Handshake status 400 Commented Aug 25, 2017 at 7:10
  • 2
    Which websocket package are you using? If it's websocket, that seems seriously out of date. Try websockets or websocket-client instead. Commented Aug 25, 2017 at 7:11
  • I am using websocket-client, installed with pip install websocket-client on python 3.6 Commented Aug 25, 2017 at 7:13

2 Answers 2

6

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())
Sign up to request clarification or add additional context in comments.

1 Comment

This just saved me a few days of fighting websockets while connecting to them via proxy. Thx.
1

For me the option from the question seems to work:

from websocket import create_connection
import ssl

ws = create_connection("wss://echo.websocket.org", sslopt={"cert_reqs": ssl.CERT_NONE})

ws.send("python hello!")
print (ws.recv())
ws.close()

See also here: https://github.com/websocket-client/websocket-client#faq

Note: I'm using win7 with python 3.6.5 with following packages installed(pip):

  • simple-websocket-server==0.4.0
  • websocket-client==0.53.0
  • websockets==6.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.