python中的ssl error怎么解决
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中的ssl error怎么解决相关的知识,希望对你有一定的参考价值。
参考技术A python安装完毕后,提示找不到ssl模块:[root@localhost ~]# python2.7.5
Python 2.7.5 (default, Jun 3 2013, 11:08:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python27/lib/python2.7/ssl.py", line 60, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: No module named _ssl
>>>
2. 查看openssl安装包,发现缺少openssl-devel包
[root@localhost ~]# rpm -aq|grep openssl
openssl-0.9.8e-20.el5
openssl-0.9.8e-20.el5
[root@localhost ~]#
3. yum安装openssl-devel
[root@localhost ~]# yum install openssl-devel -y
#查看安装结果
[root@localhost ~]# rpm -aq|grep openssl
openssl-0.9.8e-26.el5_9.1
openssl-0.9.8e-26.el5_9.1
openssl-devel-0.9.8e-26.el5_9.1
openssl-devel-0.9.8e-26.el5_9.1
4. 重新编译python
#修改Setup文件
vi /usr/software/Python-2.7.5/Modules/Setup
#修改结果如下:
# 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
5. 重新编译
make
make install
6. 测试,已可正常使用。
[root@localhost ~]# python2.7.5
Python 2.7.5 (default, Jun 3 2013, 14:56:13)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>本回答被提问者采纳 参考技术B 忽略不受信的证书就行
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
使用PHPMailer 中的报错解决 "Connection failed. Error #2: stream_socket_client(): SSL operation failed w
PHPMailer项目地址:https://github.com/PHPMailer/PHPMailer
项目中用到PHPMailer,使用过程中报错:"Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:"
由于我用的第三方smtp是ssl链接,所以需要再添加一些参数:
$mail->SMTPOptions = array( ‘ssl‘ => array( ‘verify_peer‘ => false, ‘verify_peer_name‘ => false, ‘allow_self_signed‘ => true ) );官方是这样说明的:"
This is covered in the troubleshooting docs. PHP 5.6 verifies SSL certificates by default, and if your cert doesn‘t match, it will fail with this error. The correct solution is to fix your SSL config - it‘s not PHP‘s fault!
If you‘re sending on localhost you can use
isMail()
and it won‘t go through an encryption layer at all."翻译如下:
疑难解答文档中介绍了这一点。默认情况下,PHP 5.6验证SSL证书,如果您的证书不匹配,则会出现此错误。正确的解决方案是修复您的SSL配置 - 这不是PHP的错误! 如果您正在本地发送,您可以使用isMail(),而不会通过加密层。
可是,我用的是php7.1啊!可见,php7及以上版本保留了php5.6这一特点。加了上述参数后问题解决了。
不过,官方的建议是,为了安全起见,还是建议大家效验ssl证书的。关于PHP是如何效验ssl证书的,请参考:https://secure.php.net/manual/en/context.ssl.php
在PHPMailer里效验证书的方法,是有给出配置的:
$mail->SMTPOptions = array ( ‘ssl‘ => array( ‘verify_peer‘ => true, ‘verify_depth‘ => 3, ‘allow_self_signed‘ => true, ‘peer_name‘ => ‘smtp.example.com‘, ‘cafile‘ => ‘/etc/ssl/ca_cert.pem‘, ) );
出处:https://github.com/PHPMailer/PHPMailer/issues/368
以上是关于python中的ssl error怎么解决的主要内容,如果未能解决你的问题,请参考以下文章