Search code examples
pythonhttpsopensslpki

Python example to access a php site(HTTPS) protected by PKI


I am looking for a example code for how to implement a Python application to communicate with a php site over HTTPS and use PKI protection.

I probably will use pyOpenSSL and httplib.HTTPSConnection. My question is where can I find a site that uses PKI for authentication? (would Github do this?). Also, is there any sample code for how to implement? Please advise, thanks.


Solution

  • I believe all you need to do is use httplib.HTTPSConnection like you said. I tried the following and it worked for me.

    Example:

    
        import httplib
        HOSTNAME = 'login.yahoo.com'
        conn = httplib.HTTPSConnection(HOSTNAME)
        conn.putrequest('GET', '/')
        conn.endheaders()
        response = conn.getresponse()
        print response.read()
    
    

    Make note that this does not do any verification of the server’s certificate.

    If you want to verify the certificate then you may want to use pyOpenSSL. My preferred option would actually be to extend urllib2. An example of this can be found in the following article.

    Reference:
    http://www.noah.org/wiki/Python_HTTPS_and_SSL
    http://docs.python.org/library/httplib.html