我正在尝试使用身份验证通过 HTTPS 连接建立成功的通信。我在 Ubuntu 12.04 上使用带有 Django 1.4 的 Python 2.7。
我关注的 API 文档对身份验证有特定要求。包括Authentication
您将在下面找到的标题并发送证书信息。
这是代码:
import httplib
import base64
HOST = 'some.host.com'
API_URL = '/some/api/path'
username = '1234'
password = '5678'
auth_value = base64.b64encode('WS{0}._.1:{1}'.format(username, password))
path = os.path.join(os.path.dirname(__file__), 'keys/')
pem_file = '{0}WS{1}._.1.pem'.format(path, username)
xml_string = '<some><xml></xml><stuff></stuff></some>'
headers = { 'User-Agent' : 'Rico',
'Content-type' : 'text/xml',
'Authorization' : 'Basic {0}'.format(auth_value),
}
conn = httplib.HTTPSConnection(HOST, cert_file = pem_file)
conn.putrequest("POST", API_URL, xml_string, headers)
response = conn.getresponse()
我收到以下错误:
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
77. return view_func(*args, **kwargs)
File "/home/tokeniz/tokeniz/gateway_interface/views.py" in processPayment
37. processCreditCard = ProcessCreditCard(token, postHandling)
File "/home/tokeniz/tokeniz/gateway_interface/credit_card_handling.py" in __init__
75. self.processGateway()
File "/home/tokeniz/tokeniz/gateway_interface/credit_card_handling.py" in processGateway
95. gateway = Gateway(self)
File "/home/tokeniz/tokeniz/gateway_interface/first_data.py" in __init__
37. self.postInfo()
File "/home/tokeniz/tokeniz/gateway_interface/first_data.py" in postInfo
245. response = conn.getresponse()
File "/usr/lib/python2.7/httplib.py" in getresponse
1018. raise ResponseNotReady()
Exception Type: ResponseNotReady at /processPayment/
Exception Value:
为什么我会收到此错误?
更新 1:我一直在使用.pem
他们给我的文件(链接点网关),但已经读到证书文件应该包含证书和 RSA 私钥。那是对的吗?我尝试发送.pem
包含两者的文件并收到以下错误:
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
77. return view_func(*args, **kwargs)
File "/home/tokeniz/tokeniz/gateway_interface/views.py" in processPayment
37. processCreditCard = ProcessCreditCard(token, postHandling)
File "/home/tokeniz/tokeniz/gateway_interface/credit_card_handling.py" in __init__
75. self.processGateway()
File "/home/tokeniz/tokeniz/gateway_interface/credit_card_handling.py" in processGateway
95. gateway = Gateway(self)
File "/home/tokeniz/tokeniz/gateway_interface/first_data.py" in __init__
37. self.postInfo()
File "/home/tokeniz/tokeniz/gateway_interface/first_data.py" in postInfo
251. conn.request('POST', self.API_URL, self.xml_string, headers)
File "/usr/lib/python2.7/httplib.py" in request
958. self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py" in _send_request
992. self.endheaders(body)
File "/usr/lib/python2.7/httplib.py" in endheaders
954. self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py" in _send_output
814. self.send(msg)
File "/usr/lib/python2.7/httplib.py" in send
776. self.connect()
File "/usr/lib/python2.7/httplib.py" in connect
1161. self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
File "/usr/lib/python2.7/ssl.py" in wrap_socket
381. ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py" in __init__
141. ciphers)
Exception Type: SSLError at /processPayment/
Exception Value: [Errno 336265225] _ssl.c:351: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib
我不知道这是前进还是后退。
更新 2:我在创建连接对象时尝试同时传递证书文件和密钥文件。
conn = httplib.HTTPSConnection(HOST, cert_file = pem_file, key_file = key_file)
我收到以下错误:
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
77. return view_func(*args, **kwargs)
File "/home/tokeniz/tokeniz/gateway_interface/views.py" in processPayment
37. processCreditCard = ProcessCreditCard(token, postHandling)
File "/home/tokeniz/tokeniz/gateway_interface/credit_card_handling.py" in __init__
75. self.processGateway()
File "/home/tokeniz/tokeniz/gateway_interface/credit_card_handling.py" in processGateway
95. gateway = Gateway(self)
File "/home/tokeniz/tokeniz/gateway_interface/first_data.py" in __init__
37. self.postInfo()
File "/home/tokeniz/tokeniz/gateway_interface/first_data.py" in postInfo
252. conn.request('POST', self.API_URL, self.xml_string, headers)
File "/usr/lib/python2.7/httplib.py" in request
958. self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py" in _send_request
992. self.endheaders(body)
File "/usr/lib/python2.7/httplib.py" in endheaders
954. self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py" in _send_output
814. self.send(msg)
File "/usr/lib/python2.7/httplib.py" in send
776. self.connect()
File "/usr/lib/python2.7/httplib.py" in connect
1161. self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
File "/usr/lib/python2.7/ssl.py" in wrap_socket
381. ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py" in __init__
141. ciphers)
Exception Type: SSLError at /processPayment/
Exception Value: [Errno 336265225] _ssl.c:351: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib
如果我尝试合并证书文件和密钥文件并将其作为证书参数发送,我会收到相同的错误。