Python请求:NewConnectionError、urllib3,使用cert和verify属性

2024-10-01 00:14:39 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在开发的程序涉及在银行DMS服务器中发布文档。他们向我提供了.cer格式的服务器证书,我已将其插入代码中的verify变量中。他们还提供了客户端id和密码,我必须将它们嵌入头本身。我生成了自签名的客户端证书和私钥,并以cer格式和公钥向它们提供了客户端证书。在代码中,我还给出了客户端证书的路径和证书元组中的私钥。 在执行代码时,我遇到以下错误:

    HTTPSConnectionPool(host='apimuat.xxxbank.com', port=9095): Max retries exceeded with url: /doc-mgmt/v1/uploadDoc (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fb01bd8a160>: Failed to establish a new connection: [Errno 60] Operation timed out'))


File "/Users/fpl_mayank/Documents/FPL/python-virtual-env/uploadDocApi/server.py", line 164, in main
    result = requests.post(url,
  File "/Users/fpl_mayank/Documents/FPL/python-virtual-env/uploadDocApi/server.py", line 189, in <module>
    main()

我已经用'https://postman-echo.com/post“没有提到证书和验证,只是为了检查我的请求是否通过。那里工作得很好

这是我使用请求函数的代码片段

url='https://apimuat.xxxbank.com:9095/doc-mgmt/v1/uploadDoc'
headers = {"Content-Type": "application/json", "client_id":"af197b22539647fba4db8b971b43e38", 
           "client_secret":"c1AA406e24074d8887954472C78a924"}
data = req
result = requests.post(url,
             data=data,
             headers=headers,
             cert=('/Users/fpl_mayank/Documents/FPL/python-virtual- 
env/uploadDocApi/keystore/dms_csr_certificate_self.cer','/Users/fpl_mayank/Documents/FPL/python-virtual-env/uploadDocApi/keystore/dms_private_key.key'),
             verify='/Users/fpl_mayank/Documents/FPL/python-virtual-env/uploadDocApi/truststore/APIM-UAT.cer'
             )
        
        res = result.json()

在apidoc中提到,将在bw客户端和服务器上实现双向SSL身份验证。此外,我还为此项目制作了虚拟环境。请帮忙。在我的公司里,我是第一个使用python编写API的人,所以解决问题的唯一方法就是通过良好的ol stackoverflow


Tags: 代码服务器envcomurl客户端virtualusers
1条回答
网友
1楼 · 发布于 2024-10-01 00:14:39

所以我解决了这个问题。idk是什么解决了这个问题的,但请确保在使用api时,根据要求从您的网络中获取端点的ip白名单,他们也一样。我还发送了带有标识和空格的格式化json请求,所以请确保将json保留在一行中

相关问题 更多 >