gpt4 book ai didi

REST请求中基于python证书的身份验证

转载 作者:行者123 更新时间:2023-12-05 00:59:52 25 4
gpt4 key购买 nike

我尝试在 python 中向提供 REST api 的给定服务器发送带有基于证书的身份验证的 REST 请求,但经过数小时的搜索和尝试,我认为我需要帮助。

我有来自上述服务器的签名证书和该证书的 key 。服务器本身也提供 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
是否可以使用该库运行基于证书的身份验证?

最佳答案

我能够在“请求”库的帮助下运行它。

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())

就这么简单

关于REST请求中基于python证书的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51891451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com