尝试使用websocket-client建立https连接时出现SSL错误

时间:2014-05-27 23:27:17

标签: python ssl websocket

我试图在python中编写一个websocket程序来监听服务器消息并打印它们。由于我是websockets的新手,我尝试使用websocket-client github页面上给出的代码开始。特别是像API这样的javascript。但是,我收到了这个错误:

  

[Errno 1] _ssl.c:504:错误:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败

我想使用https,但我不希望ssl验证无法标记错误。我试图在程序中搜索我可以指定的方法,但我找不到任何关于此的文献。请帮忙!

class websocketIO:
    def __init__(self, protocol, host, port):
        self.ws = None
        self.openCallback = None
        self.protocol = "ws"
        if protocol == "https:":
            self.protocol = "wss"
        self.host = host
        self.port = port
        self.messages = {}


    def open(self, callback):
        print self.protocol + "://" + self.host + ":" + self.port
        self.ws = websocket.WebSocketApp(self.protocol + "://" + self.host + ":" + self.port, on_message = self.on_message, on_error = self.on_error, on_close = self.on_close)
        self.openCallback = callback
        self.ws.on_open = self.on_open;
        self.ws.run_forever()

    def on_open(self, ws):
        threading.start_new_thread(self.openCallback, ())

    def on_message(self, ws, message):
        if isinstance(message, str):
            msg = json.loads(message)
            if msg['func'] in self.messages:
                self.messages[msg['func']](msg['data'])
            else:
                print "Error: message is not a binary string"

    def run(self, *args):
        self.ws.on_open = args[0]

    def on(self, name, callback):
        self.messages[name] = callback

    def emit(self, name, data):
        message = {'func': name, 'data': data}
        self.ws.send(json.dumps(message))

def main():
    global wsio
    wsio = websocketIO("https:", "localhost", "9090")
    wsio.on('setupDisplayConfiguration', setupDisplayConfiguration)
    wsio.on('initialize', initialize)
    wsio.open(on_open)

def on_open():
    global wsio
    wsio.emit('Message1', {'Test': "Data"});
    print "Message Sent"

def setupDisplayConfiguration(data):
    print (data['host'] + ":" + str(data['port']))

def initialize(data):
    print (data['address'])

main()

1 个答案:

答案 0 :(得分:0)

您通过SSL与客户端连接到localhost。我怀疑您在localhost上使用证书,该证书可以针对您的可信CA进行验证,并且还匹配localhost的主机名。你检查过了吗?