ssl.SSLError:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:777)

时间:2018-03-13 13:28:53

标签: python-3.x ssl websocket qliksense

我目前正在使用websocket-client处理python到QlikSense服务器连接。对于身份验证,我使用了Qlik sense服务器生成的证书。

即使我遵循此link中提到的相同代码逻辑,但仍然出错。

from websocket import create_connection

def conn(senseHost, userDirectory, userId, privateKeyPath):

    # self.url = "wss://" + senseHost + ":4747/app/" # invalid
    url = "wss://" + senseHost + ":4747/app"  # valid
    ca = open(privateKeyPath + "root.pem").read()
    cer = open(privateKeyPath + "client.pem").read()
    key = open(privateKeyPath + "client_key.pem").read()
    certs = ({"ca": ca,
              "cert": cer,
              "key": key})
    # import pdb
    # pdb.set_trace()
    # ERROR raised here.
    ws = create_connection(url, sslopt=certs,
                                header={'X-Qlik-User: UserDirectory=%s; UserId=%s' % (userDirectory, userId)})
    session = self.ws.recv()
    return session

# below code has specific perameters.
c = conn("blablah.com","XYZ","ME","path/to/cert/")

我使用pdb追踪错误,

-> ws = create_connection(url,sslopt=certs,header={'X-Qlik-User: UserDirectory=%s; UserId=%s' % (userDirectory, userId)})
(Pdb) n
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)

1 个答案:

答案 0 :(得分:1)

经过4个小时的努力,我找到了一个解决方案。

def conn(senseHost, userDirectory, userId, privateKeyPath):
    url = "wss://" + senseHost + ":4747/app"  # valid
    certs = ({"ca_certs": privateKeyPath + "root.pem",
              "certfile": privateKeyPath + "client.pem",
              "keyfile": privateKeyPath + "client_key.pem",
              "cert_reqs":ssl.CERT_REQUIRED,
              "server_side": False
              })
    ssl.match_hostname = lambda cert, hostname: True
    ws = create_connection(url, sslopt=certs,
                                header={'X-Qlik-User: UserDirectory=%s; UserId=%s'% (userDirectory, userId)})