2017-10-20 133 views
0

我的Python安全的WebSocket客戶端代碼給我的異常,如下所示:如何創建Python安全websocket客戶端請求?

[SSL:CERTIFICATE_VERIFY_FAILED]證書驗證失敗(_ssl.c:748)

我已經創建了我的私人證書,並簽名證書爲好,但我不能夠使用Python腳本如下連接到它:

import json 
from websocket import create_connection 


class subscriber: 
    def listenForever(self): 
    try: 
     # ws = create_connection("wss://localhost:9080/websocket") 
     ws = create_connection("wss://nbtstaging.westeurope.cloudapp.azure.com:9090/websocket") 
     ws.send("test message") 
     while True: 
      result = ws.recv() 
      result = json.loads(result) 
      print("Received '%s'" % result) 

     ws.close() 
    except Exception as ex: 
     print("exception: ", format(ex)) 


try: 
    subscriber().listenForever() 
except: 
    print("Exception occured: ") 

我HTTPS /在龍捲風蟒蛇WSS服務器腳本如下:

import tornado.web 
import tornado.websocket 
import tornado.httpserver 
import tornado.ioloop 
import os 
import ssl 

ssl_root = os.path.join(os.path.dirname(__file__), 'ssl1_1020') 


class WebSocketHandler(tornado.websocket.WebSocketHandler): 
    def check_origin(self, origin): 
     return True 

    def open(self): 
     pass 

    def on_message(self, message): 
     self.write_message("Your message was: " + message) 
     print("message received: ", format(message)) 

    def on_close(self): 
     pass 


class IndexPageHandler(tornado.web.RequestHandler): 
    def get(self): 
     self.render("index.html") 


class Application(tornado.web.Application): 
    def __init__(self): 
     handlers = [ 
      (r'/', IndexPageHandler), 
      (r'/websocket', WebSocketHandler), 
     ] 

     settings = { 
      'template_path': 'templates' 
     } 
     tornado.web.Application.__init__(self, handlers, **settings) 


ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) 
ssl_ctx.load_cert_chain(ssl_root+"/server.crt", 
        ssl_root + "/server.pem") 


if __name__ == '__main__': 
    ws_app = Application() 
    server = tornado.httpserver.HTTPServer(ws_app, ssl_options=ssl_ctx,) 
    server.listen(9081, "0.0.0.0") 
    print("server started...") 
    tornado.ioloop.IOLoop.instance().start() 
用於創建SSL簽名證書

步驟:同時使連接,以確保網絡插口網址如下忽略證書請求

openssl genrsa -des3 -out server.key 1024 
openssl rsa -in server.key -out server.pem 
openssl req -new -nodes -key server.pem -out server.csr 
openssl x509 -req -days 365 -in server.csr -signkey server.pem -out server.crt 
+0

你使用MacOS嗎?這很可能是系統權限的問題,您可能需要將證書添加到鑰匙串中...您可能需要檢查此帖https://stackoverflow.com/questions/41691327/ssl-sslerror-ssl-certificate-驗證失敗證書驗證失敗ssl-c – securecurve

+0

不,這是所有Linux的Ubuntu的16.04環境。 –

+1

有類似問題的人:https://stackoverflow.com/questions/27835619/urllib-and-ssl-certificate-verify-failed-error – securecurve

回答

2

終於讓我找到的解決方案,我更新了Python客戶端腳本:

ws = websocket.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE}) 
     ws.connect("wss://xxx.com:9090/websocket") 
1

如果有人對未來感到好奇爲什麼wss python服務器失敗是因爲龍捲風文檔中的這個權限:

當使用帶有自簽名證書的安全websocket連接(wss://)時,來自瀏覽器的連接可能會失敗,因爲它想顯示「接受此證書」對話框,但無處顯示它。在websocket連接成功之前,您必須首先使用相同的證書訪問常規HTML頁面以接受它。