简体   繁体   中英

Python Websocket secure [SSL: CERTIFICATE_VERIFY_FAILED] (_ssl.c:777)

I'm making a program that needs to receive real time messages from a WebSocket secure server.

I tried doing this task using JavaScript and it works beautifully!
But JavaScript doesn't satisfy the needs of my project. So I'm trying to do the same thing in Python. But no success.

The error I'm struggling to fix is:

[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)

I assume that JavaScript didn't have this problem because it uses the browser to certify the connection.

What I'm using:

  • Python 3.6.4
  • Anaconda3
  • Windows 10

I really don't know what to do. I already searched a lot.
Can I solve this by changing the code?
Should I use another lib to make the connection?
Is the problem in Python or Anaconda?
Is it some newbie error that I can't figure out?

Here's my Python code:

import websocket
import websocket
import threading
import time

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    def run(*args):
        for i in range(3):
            time.sleep(1)
            data = ws.recv()
            print(data)
        time.sleep(1)
        ws.close()
        print("thread terminating...")
    thread.start_new_thread(run, ())



websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://stream.binance.com:9443/ws/bnbbtc@ticker",
                            on_message = on_message,
                            on_error = on_error,
                            on_close = on_close)
ws.on_open = on_open

ws.run_forever()

If someone is interested, here is my JavaScript code:

var socket = new WebSocket('wss://hostname:port/dir/dir@ticker');

socket.onopen = function(event) 
{
    console.log('connected');
}


socket.onmessage = function(e){
   var server_message = e.data;
   console.log(server_message);
}

It's an error in Anaconda, try to remove certifi: conda remove certifi .

If it doesn't work, you can get more infomation from this issue: https://github.com/ContinuumIO/anaconda-issues/issues/494#issuecomment-155097614

Or you can just disable ssl verification in python script:

import ssl
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM