我的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
你使用MacOS嗎?這很可能是系統權限的問題,您可能需要將證書添加到鑰匙串中...您可能需要檢查此帖https://stackoverflow.com/questions/41691327/ssl-sslerror-ssl-certificate-驗證失敗證書驗證失敗ssl-c – securecurve
不,這是所有Linux的Ubuntu的16.04環境。 –
有類似問題的人:https://stackoverflow.com/questions/27835619/urllib-and-ssl-certificate-verify-failed-error – securecurve