Python HTTPS/SSL Windows How To
时间:2006-08-02 来源:linxh
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.
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.
Ingredients:
M2Crypto from Ng Pheng Siong. the Windows BINARY package.
OpenSSL
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.