Search code examples
pythonsslhttplib

SSL error while connecting with python httplib and pem file


I am trying to connect to an HTTPS WS with a python client using PEM file and httplib

here is the code

# HTTPS connection with python
#!/usr/bin/env python

import httplib , urllib 

CERTFILE = 'path_to_pem_file'
hostname = 'IP_address:Port_number'

headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}

Json_data = {
    "amountTransaction": {
        some json data .. 
    }
}

params = urllib.urlencode(Json_data)

conn = httplib.HTTPSConnection(
    hostname,
    key_file = CERTFILE,
    cert_file = CERTFILE 
)


conn.request("POST", '/url_to_call', params, headers)



print conn.getreponse().read()

print response.status, response.reason
conn.close()

but I am receiving the following errror,

SSLError: [Errno 336265225] _ssl.c:354: error:140B0009:SSL        
routines:SSL_CTX_use_PrivateKey_file:PEM lib

could you please check what's wrong


Solution

  • cert_file and key_file are used for client authentication against the server and must contain the certificate and the matching private key. I would interpret the error message, that either there is no key in the PEM file, it does not match the certificate or it is password protected and thus cannot be read.