带有密码密钥的.PEM证书的Python SSL错误

时间:2017-08-28 21:38:48

标签: python-2.7 python-3.x openssl python-requests

您好我是python的新手,并试图对API使用请求进行休息调用。

我的代码是

import requests
import json
from pprint import pprint

#json_file='a.json'
json_file='apiInput.json'
hdr_key=[]
hdr_value=[]
json_data=open(json_file)
data = json.load(json_data)
request_body={}
#pprint(data)
json_data.close()
request_data = data['request1']
request_header=request_data['header-data']
url=request_header['url']
cert='/Somepath/clientid.pem'

if 'body' in request_data:
    request_body=request_data['body']

r = requests.get(url, headers=request_header, cert=('/Somepath/clientid.pem','weblogic'))
print r.status_code
print r.json()

我收到以下错误

Traceback (most recent call last):
  File "/Users/tsu892/Desktop/Office/Pythone-work/ASR-pythone/ASR-Application/asrmain.py", line 29, in <module>
    r = requests.get(url, headers=request_header, cert=('/Users/tsu892/Desktop/Office/ASR/asr-asrint-branch/rtmqa-clientid.pem','weblogic'))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/adapters.py", line 407, in send
    self.cert_verify(conn, request.url, verify, cert)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/adapters.py", line 251, in cert_verify
    "invalid path: {0}".format(conn.key_file))
IOError: Could not find the TLS key file, invalid path: weblogic

我有.PEM文件和用于密钥的密码。我有一个看起来像的红宝石代码:

if url.scheme == "https"
  http.use_ssl = true
  http.key = OpenSSL::PKey::RSA.new(File.read("/Somepath/clientid.pem"), "weblogic")
  http.cert = OpenSSL::X509::Certificate.new(File.read("/Somepath/clientid.pem"))
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

end

在ruby代码中,http中的.key选项允许我使用密码打开certfile来获取密钥。我不知道如何在python中执行此操作或类似操作。 有人可以帮忙!!!!

0 个答案:

没有答案