我尝试使用基于证书的身份验证向提供REST API的特定服务器发送Python中的REST请求,但是经过几个小时的搜索和尝试后,我认为我需要帮助。
我有来自所提到服务器的已签名证书和该证书的密钥。服务器本身也为https提供证书。
我尝试了httplib.HTTPSConnection库:
import httplib import urllib
clientCert = "client.crt" clientKey = "client.key" serverCert = 'server.crt' serverApi = "/api/getExample" serverHost = "restapi.example.com" serverPort = 443 requestMethod = "POST" params = urllib.urlencode({'someId': 'myId'}) headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "application/json"}
conn = httplib.HTTPSConnection(serverHost, serverPort, key_file=clientKey, cert_file=clientCert) conn.request(requestMethod, serverApi, params, headers) response = conn.getresponse() conn.getresponse() conn.close()
我得到ssl.SSLError:SSL: CERTIFICATE_VERIFY_FAILED
使用该库可以实现基于证书的身份验证吗?
clientCrt
和clientKey
是文件路径还是包含证书和密钥的字符串?如果是前者,那么如果我只有后者,该怎么办呢? - Kai Roesner