Python无法导入ssl模块

时间:2017-06-01 06:09:29

标签: python django apache ssl xampp

在ubuntu-16.04上安装了xampp。 XAMPP根安装位置:/opt/lampp

Python 2.7.12 Django 1.11 XAMPP 5.6.30

wsgi.py设置: ```蟒

import os
import sys
import site

site.addsitedir('/opt/lampp/htdocs/dpcm/dpcm/lib/python2.7/site-packages')

sys.path.insert(0, '/opt/lampp/htdocs/dpcm')
sys.path.insert(1, '/opt/lampp/htdocs/dpcm/dpcm1')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dpcm1.settings")
os.environ.setdefault("PYTHON_EGG_CACHE", "/opt/lampp/htdocs/dpcm/.python-eggs")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# if import ssl module, it will occur an error.
import ssl

```

/opt/lampp/apache2/conf/httpd.conf内容:

Alias /bitnami/ "/opt/lampp/apache2/htdocs/"
Alias /bitnami "/opt/lampp/apache2/htdocs"

<Directory "/opt/lampp/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

WSGIScriptAlias /dpcm/ /opt/lampp/htdocs/dpcm/dpcm1/wsgi.py

#WSGIDaemonProcess /dpcm/ python-path=/opt/lampp/htdocs/dpcm:/opt/lampp/htdocs/dpcm/dpcm/lib/python2.7/site-packages
#WSGIProcessGroup /dpcm/

WSGIPythonPath /opt/lampp/htdocs/dpcm
WSGIPythonHome /opt/lampp/htdocs/dpcm/dpcm/
<Directory /opt/lampp/htdocs/dpcm/dpcm1>
    <Files wsgi.py>
        Order deny,allow
        Require all granted
    </Files>
</Directory>

Alias /static/ /opt/lampp/htdocs/dpcm/static/
<Directory /opt/lampp/htdocs/dpcm/static>
   Order allow,deny
   Allow from all
</Directory>

如果在非活动模式下导入ssl模块,则可以成功导入。 但是如果在wsgi.py或其他django项目文件中添加'import ssl',则会报告错误消息:

[Thu Jun 01 10:54:13.272126 2017] [wsgi:error] [pid 5430] [client 10.0.2.15:41534] mod_wsgi (pid=5430): Target WSGI script '/opt/lampp/htdocs/dpcm/dpcm1/wsgi.py' cannot be loaded as Python module.
[Thu Jun 01 10:54:13.272176 2017] [wsgi:error] [pid 5430] [client 10.0.2.15:41534] mod_wsgi (pid=5430): Exception occurred processing WSGI script '/opt/lampp/htdocs/dpcm/dpcm1/wsgi.py'.
[Thu Jun 01 10:54:13.272207 2017] [wsgi:error] [pid 5430] [client 10.0.2.15:41534] Traceback (most recent call last):
[Thu Jun 01 10:54:13.272230 2017] [wsgi:error] [pid 5430] [client 10.0.2.15:41534]   File "/opt/lampp/htdocs/dpcm/dpcm1/wsgi.py", line 33, in <module>
[Thu Jun 01 10:54:13.272308 2017] [wsgi:error] [pid 5430] [client 10.0.2.15:41534]     import ssl
[Thu Jun 01 10:54:13.272320 2017] [wsgi:error] [pid 5430] [client 10.0.2.15:41534]   File "/usr/lib/python2.7/ssl.py", line 97, in <module>
[Thu Jun 01 10:54:13.276754 2017] [wsgi:error] [pid 5430] [client 10.0.2.15:41534]     import _ssl             # if we can't import it, let the error propagate
[Thu Jun 01 10:54:13.276826 2017] [wsgi:error] [pid 5430] [client 10.0.2.15:41534] ImportError: /opt/lampp/lib/libssl.so.1.0.0: version `OPENSSL_1.0.1' not found (required by /opt/lampp/htdocs/dpcm/dpcm/lib/python2.7/lib-dynload/_ssl.x86_64-linux-gnu.so)

正常情况下,它会将.so链接到/usr/lib,但会在/opt/lampp/lib中找到, 所以我认为它是env变量问题。 在shell脚本中添加以下代码:lampp

LD_LIBRARY_PATH='/usr/lib'
export LD_LIBRARY_PATH

和评论代码

# if test "$(osguess)" = "macosx"
# then
#     # Thanks to drosenbe! - oswald [3sep10]
#     if test -z $DYLD_LIBRARY_PATH
#     then
#   export DYLD_LIBRARY_PATH="$LIBRARY_PATH"
#     else
#   export DYLD_LIBRARY_PATH="$LIBRARY_PATH:$DYLD_LIBRARY_PATH"
#     fi
# else
#     # Thanks to drosenbe! - oswald [3sep10]
#     if test -z $LD_LIBRARY_PATH
#     then
#   export LD_LIBRARY_PATH="$LIBRARY_PATH"
#     else
#   export LD_LIBRARY_PATH="$LIBRARY_PATH:$LD_LIBRARY_PATH"
#     fi
# fi

这没用,是否有人和我有同样的问题?

3 个答案:

答案 0 :(得分:1)

您的Python发行版是针对与Apache发行版使用的SSL库不同的SSL库编译的。 Apache加载的那个优先,导致Python以后失败。

简而言之,您不能使用该Apache发行版,因为它与嵌入语言的任何Apache模块不兼容,例如Python,PHP,Perl等,语言运行时使用系统SSL库。请改用系统Apache发行版。

答案 1 :(得分:0)

导入后

site.addsitedir('/opt/lapmpp/htdocs/dpcm/dpcm/lib/python2.7/site-packages')

/ opt / lampp 而不是 / opt / lapmpp

请尝试此更改。

答案 2 :(得分:0)

请检查这一点,它可能会有所帮助。 Why use sys.path.append(path) instead of sys.path.insert(1, path)?