Car*_*uez 2 python authentication rest certificate
我尝试使用基于证书的身份验证在 python 中向提供 REST api 的给定服务器发送 REST 请求,但经过数小时的搜索和尝试,我想我需要帮助。
我有来自上述服务器的签名证书和该证书的密钥。服务器本身也为 https 提供证书。
我尝试了图书馆 httplib.HTTPSConnection:
导入 httplib
导入 urllib
clientCert = "client.crt"
clientKey = "client.key"
serverCert = 'server.crt'
serverApi = "/api/getExample"
serverHost = "restapi.example.com"
服务器端口 = 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)
响应 = conn.getresponse()
conn.getresponse()
conn.close()
我得到ssl.SSLError: SSL: CERTIFICATE_VERIFY_FAILED
是否可以使用该库运行基于证书的身份验证?
我能够在“请求”库的帮助下运行它。
import json
import requests
clientCrt = "cc.crt"
clientKey = "ck.key"
url = "https://example.com/api"
payload = { "someId": "myID" }
certServer = 'cs.crt'
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), verify=certServer,
headers=headers, cert=(clientCrt, clientKey))
print(r.status_code)
print(r.json())
Run Code Online (Sandbox Code Playgroud)
就那么简单
| 归档时间: |
|
| 查看次数: |
11768 次 |
| 最近记录: |