How to import _ssl in python 2.7.6?

2024/11/15 18:55:47

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 processnet_flag = net_api_process()File "./script/core/netbase/interface.py", line 96, in net_api_processflag1 = network.instance().process()File "./script/core/netbase/network.py", line 271, in processif network.process(max_events):File "./script/core/netbase/network.py", line 75, in on_incomin_streamself.on_package(buf)File "./script/core/netbase/network.py", line 78, in on_packageself.f_on_package(self, buf)File "./script/client/behavior.py", line 68, in on_packagehandler.OnPackage(pack, cmd, conn.m_uid, conn)File "./script/client/handler.py", line 288, in OnPackagefunc(uid, conn, pack)File "./script/logic/user_info/modify.py", line 365, in OnModBaseInfoModBaseInfo(uid, conn, seq, json_str)File "./script/logic/user_info/modify.py", line 385, in ModBaseInfomodify_pub.Start()File "./script/logic/user_info/modify.py", line 253, in Startimport _ssl
ImportError: No module named _ssl
Answer

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.

https://en.xdnf.cn/q/71854.html

Related Q&A

Unexpected Indent error in Python [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 4 years ago.I have a simple piece of code that Im not understanding where my er…

pyshark can not capture the packet on windows 7 (python)

I want to capture the packet using pyshark. but I could not capture the packet on windows 7.this is my python codeimport pyshark def NetCap():print capturing...livecapture = pyshark.LiveCapture(interf…

How to get the Signal-to-Noise-Ratio from an image in Python?

I am filtering an image and I would like to know the SNR. I tried with the scipy function scipy.stats.signaltonoise() but I get an array of numbers and I dont really know what I am getting.Is there an…

Python and OpenCV - Cannot write readable avi video files

I have a code like this:import numpy as np import cv2cap = cv2.VideoCapture(C:/Users/Hilman/haatsu/drive_recorder/sample/3.mov)# Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_…

Python as FastCGI under windows and apache

I need to run a simple request/response python module under an existing system with windows/apache/FastCGI.All the FastCGI wrappers for python I tried work for Linux only (they use socket.fromfd() and …

Scrapy shell return without response

I have a little problem with scrapy to crawl a website. I followed the tutorial of scrapy to learn how crawl a website and I was interested to test it on the site https://www.leboncoin.fr but the spide…

How to replace values using list comprehension in python3?

I was wondering how would you can replace values of a list using list comprehension. e.g. theList = [[1,2,3],[4,5,6],[7,8,9]] newList = [[1,2,3],[4,5,6],[7,8,9]] for i in range(len(theList)):for j in r…

Installed PySide but cant import it: no module named PySide

Im new to Python. I have both Python 2.7 and Python 3 installed. I just tried installing PySide via Homebrew and got this message:PySide package successfully installed in /usr/local/lib/python2.7/sit…

How to run SQLAlchemy on AWS Lambda in Python

I preapre very simple file for connecting to external MySQL database server, like below:from sqlalchemy import *def run(event, context):sql = create_engine(mysql://root:[email protected]/scraper?chars…

saving csv file to s3 using boto3

I am trying to write and save a CSV file to a specific folder in s3 (exist). this is my code: from io import BytesIO import pandas as pd import boto3 s3 = boto3.resource(s3)d = {col1: [1, 2], col2: […