title: SSL Connection Using .pem Certificate With Python
body: <p>I'm trying to establish successful communication over an HTTPS connection using authentication.  I'm using Python 2.7 w/ Django 1.4 on Ubuntu 12.04.</p>
  
  <p>The API documentation I'm following has specific requirements for authentication.  Including the <code>Authentication</code> header you'll find below and sending certificate information.</p>
  
  <p>This is the code:</p>
  
  <pre><code>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 = '&lt;some&gt;&lt;xml&gt;&lt;/xml&gt;&lt;stuff&gt;&lt;/stuff&gt;&lt;/some&gt;'
  
  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()
  </code></pre>
  
  <p>I'm getting the following error:</p>
  
  <pre><code>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: 
  </code></pre>
  
  <p>Why am I getting this error?</p>
  
  <p>UPDATE 1:
  I've been using the <code>.pem</code> file they gave me (Link Point Gateway) but have read that the certificate file should contain both the certificate and the RSA private key.  Is that correct?  I tried to send a <code>.pem</code> file containing both and received the following error:</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>I can't tell if this is a step forward or backward.</p>
  
  <p>UPDATE 2:
  I've tried to pass both a certificate file and a key file when creating the connection object.</p>
  
  <pre><code>conn = httplib.HTTPSConnection(HOST, cert_file = pem_file, key_file = key_file)
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>If I try and combine the certificate file and key file and send it as the certificate argument I receive the same error.</p>