尝试使用websocketclien建立https连接时发生SSL错误

2024-10-01 17:34:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python编写一个websocket程序,它监听服务器消息并打印它们。因为我是websockets的新手,所以我试图从使用websocket-clientgithub页面上给出的代码开始。尤其是类似javascript的API。但是,我得到一个错误:

[Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

我想使用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()

Tags: selfhostmessagedatawsonportdef

热门问题