python import _ssl_如何在python 2.7.6中导入_ssl?

在基于Python 2.7.6的BaseHTTPServer上,开发者遇到无法导入ssl模块的问题,导致https支持失效。经过排查,发现是系统缺少openssl库。解决方案包括:以root用户身份安装openssl和libssl-dev,或者手动下载openssl源码编译安装,并重新编译支持ssl的Python。通过这些步骤,成功解决了服务器导入ssl模块的错误,实现了https的支持。
摘要由CSDN通过智能技术生成

My http server is based on BaseHTTPServer with Python 2.7.6. Now I want it to support ssl transportation, so called https.

I have installed pyOpenSSL and recompiled python source code with ssl support. And it does work when I try import ssl in my python interpreter, but it doesn't work when I run the code on my server. The error log is like this:

import _ssl # if we can't import it, let the error propagate

It looks quite strange, doesn't it? My operating system is Debian Linux distribution. I have tried all kinds of ways which I can find on the Internet for days, anyone can help me get out of this trouble?

I tried to "import _ssl" in server code directly, but it reminds me this:

>>>callstack

Traceback (most recent call last):

File "./script/main.py", line 85, in process

net_flag = net_api_process()

File "./script/core/netbase/interface.py", line 96, in net_api_process

flag1 = network.instance().process()

File "./script/core/netbase/network.py", line 271, in process

if network.process(max_events):

File "./script/core/netbase/network.py", line 75, in on_incomin_stream

self.on_package(buf)

File "./script/core/netbase/network.py", line 78, in on_package

self.f_on_package(self, buf)

File "./script/client/behavior.py", line 68, in on_package

handler.OnPackage(pack, cmd, conn.m_uid, conn)

File "./script/client/handler.py", line 288, in OnPackage

func(uid, conn, pack)

File "./script/logic/user_info/modify.py", line 365, in OnModBaseInfo

ModBaseInfo(uid, conn, seq, json_str)

File "./script/logic/user_info/modify.py", line 385, in ModBaseInfo

modify_pub.Start()

File "./script/logic/user_info/modify.py", line 253, in Start

import _ssl

ImportError: No module named _ssl

解决方案

I fixed the problem finally!

The _ssl module is a built-in module for python, but it requires openssl installed on your system.

Change to root user first!

1.Install openssl and libssl-dev.

If on debian OS

apt-get install openssl

apt-get install libssl-dev

2.Recompile python

cd Python2.7.6

./configure

make && make install

But actually, I fix the problem in this way!

1.Install openssl

I download a package from the Internet, which is openssl-1.0.2d.tar.gz

tar zxvf openssl-1.0.2d.tar.gz

cd openssl-1.0.2d

./config -fPIC //configuration:create path independent libssl.a

make && make install

2.Recompile python2.7, and make it support ssl module

tar xvf Python-2.7.6.tar

cd Python-2.7.6

vim Modules/Setup.dist

find the following line and uncomment it.(use /ssl+Enter to quickly locate these lines in vim)

# Socket module helper for socket(2)

_socket socketmodule.c timemodule.c

# Socket module helper for SSL support; you must comment out the other

# socket line above, and possibly edit the SSL variable:

SSL=/usr/local/ssl

_ssl _ssl.c \

-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \

-L$(SSL)/lib -lssl -lcrypto

and then config and make, make install

./configure --enable-shared //--enable-shared option means to generate dynamic library libpython2.7.so.1.0

make && make install

Our project depends on libpython2.7.so.1.0. Now I can "import ssl" or "import _ssl" successfully in my python script or python interpreter with the new libpython2.7.so.1.0.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值