python2.5添加ssl模块
冰海 2012-03-21 11:29:13 原文出处 : http://kerwintian.blog.163.com/blog/static/357571682010411112353788
在使用GAE的Python时,第一次上传(Deploy)程序,报告说无法使用SSL模块通信。
WARNING appengine_rpc.py:399 ssl module not found. Without the ssl module, the identity of the remote host cannot be verified, and connections may NOT be secure. To fix this, please install the ssl module from http://pypi.python.org/pypi/ssl .
说明我安装的ActivePython2.5版本没有SSL模块,与GAE的通信存在安全隐患。于是动手为Python添加SSL模块。
在Google上搜索相关的内容,发现网上的教程很多,英文为主,中文也有。大体概括为以下几个步骤:
1、SSL源代码;
2、选用相关的编译器;
3、编译SSL模块并安装
说起来容易动手起来问题还是挺多的。
问题一:
按照链接下载pypi的ssl源代码后,运行:
Python setup.py build
首先提示:
running build
running build_py
running build_ext
error: Python was built with Visual Studio 2003;
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2003 was not found on this system. If you have Cygwin installed,
you can try compiling with MingW32, by passing “-c mingw32″ to setup.py.
说明我没有安装编译器,无法编译SSL。这里说需要VS2003的编译器或者使用mingw编译器。
不想为编译这个小东西安装VS,于是下载mingw编译器及相关组件处理这个问题。这里按照Compile ssl 1.15 for Python 2.5 or lower)的说明,需要下载:
1、mingw32
2、OpenSSL
3、libgw32c
我安装时mingw32只选择了g++编译器和mingw make环境。OpenSSL和libgw32c都安装到了C:\mingw目录下,反正没有文件重复,省事儿了。
然后在环境变量中将C:\mingw\bin加入PATH的最后。
再满怀欣喜的运行:
Python setup.py build –cmingw32
遇到了如下问题:
问题二
得到了好多warning和error,大体如下:
running build
running build_py
running build_ext
building ’ssl._ssl2′ extension
writing build\temp.win32-2.5\Release\ssl\_ssl2.def
C:\MinGW\bin\gcc.exe -mno-cygwin -shared -s build\temp.win32-2.5\Release\ssl\_ss
l2.o build\temp.win32-2.5\Release\ssl\_ssl2.def -LC:\GnuWin32\lib -LC:\Python25\
libs -LC:\Python25\PCbuild -lssl -lcrypto -lwsock32 -lgdi32 -lgw32c -lole32 -luu
id -lpython25 -lmsvcr71 -o build\lib.win32-2.5\ssl\_ssl2.pyd -static
build\temp.win32-2.5\Release\ssl\_ssl2.o:_ssl2.c:(.text+0xab7): undefined refere
nce to `_imp___Py_NoneStruct’
build\temp.win32-2.5\Release\ssl\_ssl2.o:_ssl2.c:(.text+0xabe): undefined refere
nce to `_imp___Py_NoneStruct’
build\temp.win32-2.5\Release\ssl\_ssl2.o:_ssl2.c:(.text+0xe7c): undefined refere
nce to `_imp___Py_NoneStruct’
build\temp.win32-2.5\Release\ssl\_ssl2.o:_ssl2.c:(.text+0xeba): undefined refere
nce to `_imp___Py_NoneStruct’
build\temp.win32-2.5\Release\ssl\_ssl2.o:_ssl2.c:(.text+0×11c6): undefined refer
ence to `_imp___Py_NoneStruct’
build\temp.win32-2.5\Release\ssl\_ssl2.o:_ssl2.c:(.text+0×1256): more undefined
references to `_imp___Py_NoneStruct’ follow
build\temp.win32-2.5\Release\ssl\_ssl2.o:_ssl2.c:(.text+0×2467): undefined refer
ence to `_imp__PyString_Type’
build\temp.win32-2.5\Release\ssl\_ssl2.o:_ssl2.c:(.text+0×246e): undefined refer
ence to `_imp__PyString_Type’
build\temp.win32-2.5\Release\ssl\_ssl2.o:_ssl2.c:(.text+0×2496): undefined refer
ence to `_imp__PyExc_TypeError’
build\temp.win32-2.5\Release\ssl\_ssl2.o:_ssl2.c:(.text+0×267f): undefined refer
ence to `_imp__PyType_Type’
collect2: ld returned 1 exit status
error: command ‘gcc’ failed with exit status 1
可以理解了,说明我这里有未定义的标识符。缺东西!
然后再上网搜!得到如下解决方案:(来源:http://oldwiki.mingw.org/index.php/Python%20extensions)
首先用pexports给python25.dll做一个导出表,libpython25.a,然后将这个导出表放到python25.lib相同的目录下。
然后再编译,编译通过了!然后安装,尝试了几个命令都不行。
Python setup.py install
Python setup.py install –cmingw32
……
都提示不过。
问题三
最后一步!再搜!这个命令的help没有做好,非常耽误时间。
最后发现了这个命令,执行即可:
python setup.py build –cmingw32 install
至此搞定!花费时间不下2个小时~麻烦
总结
好了最后总结一下具体步骤吧,这些内容主要适用于第一次搭建SSL环境的用户。
前期准备阶段
1、下载mingw编译器及相关环境和组件地址如下:
* mingw32 ** OpenSSL *** libgw32c
2、安装mingw32的g++和make环境,并将ming的bin目录添加到环境变量;将OpenSSL和libgw32c都安装或解压缩到mingw的目录下;
下载OpenSSL和libgw32c这两个包(选择"Developer files")
然后两个解压了放在一个目录(例如我是F:\GnuWin32)
3、下载pexport,地址为http://www.emmestech.com/software/pexports-0.43/pexports-0.43.zip
4、在windows/system32下找python25.dll,然后拷贝到随便一个文件夹,并在该文件夹下执行命令
pexports python25.dll > python25.def
dlltool –dllname python25.dll –def python25.def –output-lib libpython25.a
5、拷贝这个.a文件到c:\python25\libs\(就是python25.lib所在的目录)
在cmd下切换到ssl-1.15
编译安装SSL阶段
1、按照GAE的提示下载ssl模块,解压缩到文件夹(最好为英文);
2、命令行窗口进入到该文件夹,运行
python setup.py build –cmingw32 install
执行上述命令
就能安装了得到