加入收藏 | 设为首页 | 会员中心 | 我要投稿 大庆站长网 (https://www.0459zz.com/)- 科技、智能边缘云、事件网格、云计算、站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何在python 2.7.6中导入_ssl?

发布时间:2020-09-19 01:30:16 所属栏目:Python 来源:互联网
导读:我的http服务器基于BaseHTTPServer和Python 2.7.6.现在我希望它支持ssl传输,所谓的https.我已经安装了pyOpenSSL并使用ssl支持重新编译了python源代码.当我在我的python解释器中尝试导入ssl时它确实有效,但是当我在我的服务器上运行代码时它不起作用.错误日志

我的http服务器基于BaseHTTPServer和Python 2.7.6.现在我希望它支持ssl传输,所谓的https.

我已经安装了pyOpenSSL并使用ssl支持重新编译了python源代码.当我在我的python解释器中尝试导入ssl时它确实有效,但是当我在我的服务器上运行代码时它不起作用.错误日志是这样的:

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

看起来很奇怪,不是吗?我的操作系统是Debian Linux发行版.我已经尝试过各种各样的方法,我可以在互联网上找到几天,有谁可以帮助我摆脱这个麻烦?

我试图直接在服务器代码中“导入_ssl”,但它提醒我:

>>>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,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
最佳答案 我终于解决了问题!
_ssl模块是python的内置模块,但它需要在系统上安装openssl.

首先更改为root用户!
1.安装openssl和libssl-dev.
如果在debian OS上

apt-get install openssl
apt-get install libssl-dev

2.重新编译python

cd Python2.7.6
./configure
make && make install

但实际上,我以这种方式解决问题!
1.安装openssl
我从Internet下载了一个包,它是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.重新编译python2.7,并使其支持ssl模块

tar xvf Python-2.7.6.tar
cd Python-2.7.6
vim Modules/Setup.dist 

找到以下行并取消注释.(使用/ ssl Enter在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

然后配置和make,make install

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

我们的项目依赖于libpython2.7.so.1.0.现在我可以使用新的libpython2.7.so.1.0在我的python脚本或python解释器中成功“导入ssl”或“import _ssl”.

(编辑:大庆站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读