💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
| ssl 模块 | python3 | | --- | --- | | SSL 模块作用 | | | --- | --- | >主要是针对爬虫时的https协议的SSL验证 >使用ssl创建未经验证的上下文,在urlopen中需传入上下文参数 urllib.request.urlopen(full_url, context=context) 这是Python 升级到 2.7.9 之后引入的一个新特性,所以在使用urlopen打开https链接会遇到如下报错 ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777) 所以,当使用urllib.urlopen打开一个 https 链接时,需要先验证一次 SSL 证书 context = ssl._create_unverified_context() 或者导入ssl时关闭证书验证 ssl._create_default_https_context =ssl._create_unverified_context | SSL 模块安装 | | | --- | --- | >提示找不到SSL模块 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 >>> ``` 重新编译python(去掉最后4行的注释),找到编译安装包 ``` #修改Setup文件 vi /root/Python-3.6.5/Modules/Setup.dist #修改结果如下: # 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 ``` 编译安装 ``` ./configure --prefix=/usr/local/python make make install ``` 测试,已可正常使用。 ``` [root@localhost ~]# python Python 3.6.4 (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 >>> ```