让python支持ssl – IT夜班车

让python支持ssl

 

如果你的python不支持ssl的话,在跟https协议打交道的时候就会出错。如何来检测当前python是否支持ssl呢?

$ /usr/local/bin/python
Python 2.7.3 (default, Mar 10 2015, 17:21:03)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> hasattr(socket, 'ssl')
False
>>>

如果上面输出是False表示不支持,如果是True表示支持。

我上面的输出是False。

接下来就是下载openssl,从官网

www.openssl.org


然后解压

tar zxf openssl-0.9.8g.tar.gz
cd openssl-0.9.8g
./config
make
make install

接下来就是下载python的源代码,我么你选择 2.7.3

tar zxf Python-2.7.3.tgz && cd Python-2.7.3

然后更改文件 Modules/Setup.dist,把206到209这3行反注释(默认是被注释掉的),然后保存

204:# Socket module helper for SSL support; you must comment out the other
205:# socket line above, and possibly edit the SSL variable:
206:SSL=/usr/local/ssl
207:_ssl _ssl.c \
208:    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
209:    -L$(SSL)/lib -lssl -lcrypto</pre>

接下来配置编译安装python

./configure
make
make install</pre>

然后使用上面的方法检测

$ /usr/local/bin/python
Python 2.7.3 (default, Mar 10 2015, 18:41:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> hasattr(socket,'ssl')
True

 

 

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章转载自:IT夜班车,否则按侵权处理.

    分享到:

Leave a Reply

Your email address will not be published. Required fields are marked *