Python HTTPS and SSL

From Noah.org
(Redirected from Python HTTPS/SSL)
Jump to: navigation, search

Python HTTPS/SSL Windows How To

Update

This information is old. Using HTTPS in the newer versions of Python (2.x and later) is considerably easier now. Now it's simply a matter of doing the following:

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

For information on how to get SSL working on Windows check out Simon Willison's page.

   "Adding SSL support to Python on Windows is as easy as dropping a couple of DLLs and a .pyd file in to your Python DLLs directory. Grab the zip file from this page and off you go. I haven't tried it out yet but it appears to work - the socket.ssl function miraculously appeared when I installed the new files. Why is this useful? Because it opens the way for secure XML-RPC calls from Python applications..."

How to install and setup M2Crypto for the WinNT environment in three easy steps

M2Crypto will let you retrieve web pages from HTTPS servers. It's great, but it is short on documentation. I wrote these notes to help you get started.

Noah Spurrier 20011116 Ingredients:

   M2Crypto from Ng Pheng Siong. I downloaded the Windows BINARY package.
   OpenSSL -- I used the Win32 0.9.6 version from Ng's site

Step 1. Install OpenSSL

   Step 1.1 Install OpenSSL DLL files somewhere
   This is easy. Just open the the OpenSSL zip file into a directory somewhere on your file system. I put it here:
       C:\openssl-0.9.6-win32
   It should include these DLLs:
       libeay32.dll
       ssleay32.dll
   Step 1.2 Add the path to the DDLs to system Path environment
   Add the directory to your system path. Open "Control Panel | System | Advanced | Environment Variables". Edit the environment variable, Path, to include:
       ;C:\openssl-0.9.6-win32

Step 2. Install M2Crypto

   Step 2.1 Open the m2crypto zip file
   Open the zip file m2crypto-0.06-snap6-win32.zip into a package directory m2crypto-0.06-snap6.
   Step 2.2 Configure M2Crypto library with Windows specific files.
   In the m2crypto-0.06-snap6 package directory find these directories:
       M2Crypto/
       win/
   You need to copy two files from the win directory to the M2Crypto directory. Which files you copy will depend on which version of Python you are using. The win directory will have subdirectories for three versions of Python:
       py1/ ......... For Python 1.5.2
       py20/ ....... For Python 2.0
       py21/ ....... For Python 2.1
   Copy all the files from the appropriate directory into the M2Crypto directory.
   The M2Crypto directory should now include these files:
       _m2crypto.py
       _m2cryptoc.dll
   Step 2.3 Let Python see the M2Crypto library
   This is sort of what would happen if M2Crypto had a distutil (see also the site-module in Python docs). Copy the M2Crypto directory to your Python root directory. For example on my system it would be copied here:
       C:\Python21\M2Crypto
   Then in your Python root directory create pth file (a text file) called M2Crypto.pth. For example on my system it goes here:
       C:\Python21\M2Crypto.pth
   The pth file should contain a single line that points to the M2Crypto directory. For example in my pth file it has the following line:
       C:\Python21\M2Crypto

Step 3. You're done.

   Start up Python and see if it works. From the interpreter type:
       import M2Crypto
   If this does not return an error then you should be ready to go.