Q: python3使用requests模块发https请求,提示caused by ssl error, can't connect to https url because the ssl module is not available

首先在pycharm--File--Settings--Project--Project Interpreter,添加ssl,结果安装如下报错,在命令行用 pip install ssl 也是报相同的错误 :

Looking in indexes: http://pypi.douban.com/simple
Collecting ssl
Downloading http://pypi.doubanio.com/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz
ERROR: Command errored out with exit status 1:
command: /usr/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-8cbn9rjm/ssl/setup.py'"'"'; __file__='"'"'/tmp/pip-install-8cbn9rjm/ssl/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-8cbn9rjm/ssl/pip-egg-info
cwd: /tmp/pip-install-8cbn9rjm/ssl/
Complete output (6 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-8cbn9rjm/ssl/setup.py", line 33
print 'looking for', f
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(print 'looking for', f)?
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

一开始以为是本机安装了python2和python3导致的,因为上述错误是语法错误,print 'looking for', f,python3的print是作为方法,不是关键字。想修改上面的setup.py,发现在/tmp/pip-install-8cbn9rjm/ssl/setup.py找不到这个文件,估计是失败自动回滚删除了。

于是在命令行pip3 install ssl -v 打印详细信息,报错如下:

[root@ana53 yum-rpm]# pip install ssl -v
Traceback (most recent call last):
File "/usr/local/bin/pip", line 5, in <module>
from pip._internal.main import main
ImportError: No module named pip._internal.main
[root@ana53 yum-rpm]# pip3 install ssl -v
Collecting ssl
1 location(s) to search for versions of ssl:
* http://pypi.douban.com/simple/ssl/
Getting page http://pypi.douban.com/simple/ssl/
Starting new HTTP connection (1): pypi.douban.com
"GET /simple/ssl/ HTTP/1.1" 301 162
Starting new HTTP connection (1): pypi.doubanio.com
"GET /simple/ssl/ HTTP/1.1" 200 417
Analyzing links from page http://pypi.doubanio.com/simple/ssl/
Found link http://pypi.doubanio.com/packages/3a/c2/846a19d1572ec6cb8ac438d58a898de8926d32e13f0355cdf4ab00864b5f/ssl-1.15.tar.gz#md5=81ea8a1175e437b4c769ae65b3290e0c (from http://pypi.doubanio.com/simple/ssl/), version: 1.15
Found link http://pypi.doubanio.com/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz#md5=fb12d335d56f3c8c7c1fefc1c06c4bfb (from http://pypi.doubanio.com/simple/ssl/), version: 1.16
Using version 1.16 (newest of versions: 1.15, 1.16)
"GET /packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz HTTP/1.1" 200 33052
Downloading http://pypi.doubanio.com/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz
Downloading from URL http://pypi.doubanio.com/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz#md5=fb12d335d56f3c8c7c1fefc1c06c4bfb (from http://pypi.doubanio.com/simple/ssl/)
Running setup.py (path:/tmp/pip-build-la62j81l/ssl/setup.py) egg_info for package ssl
Running command python setup.py egg_info
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-la62j81l/ssl/setup.py", line 33
print 'looking for', f
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(print 'looking for', f)?
Cleaning up...
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-la62j81l/ssl/
Exception information:
Traceback (most recent call last):
File "/opt/python36/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/opt/python36/lib/python3.6/site-packages/pip/commands/install.py", line 324, in run
requirement_set.prepare_files(finder)
File "/opt/python36/lib/python3.6/site-packages/pip/req/req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "/opt/python36/lib/python3.6/site-packages/pip/req/req_set.py", line 634, in _prepare_file
abstract_dist.prep_for_dist()
File "/opt/python36/lib/python3.6/site-packages/pip/req/req_set.py", line 129, in prep_for_dist
self.req_to_install.run_egg_info()
File "/opt/python36/lib/python3.6/site-packages/pip/req/req_install.py", line 439, in run_egg_info
command_desc='python setup.py egg_info')
File "/opt/python36/lib/python3.6/site-packages/pip/utils/__init__.py", line 707, in call_subprocess
% (command_desc, proc.returncode, cwd))
pip.exceptions.InstallationError: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-la62j81l/ssl/
Looking up "https://pypi.python.org/pypi/pip/json" in the cache
Returning cached "301 Moved Permanently" response (ignoring date and etag information)
Looking up "https://pypi.org/pypi/pip/json" in the cache
Current age based on date: 77343
Freshness lifetime from max-age: 900
Starting new HTTPS connection (1): pypi.org
There was an error checking the latest version of pip
Traceback (most recent call last):
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/adapters.py", line 423, in send
timeout=timeout
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 583, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 257, in _get_conn
return conn or self._new_conn()
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 808, in _new_conn
raise SSLError("Can't connect to HTTPS URL because the SSL "
pip._vendor.requests.packages.urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available. During handling of the above exception, another exception occurred: Traceback (most recent call last):
File "/opt/python36/lib/python3.6/site-packages/pip/utils/outdated.py", line 126, in pip_version_check
headers={"Accept": "application/json"},
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 488, in get
return self.request('GET', url, **kwargs)
File "/opt/python36/lib/python3.6/site-packages/pip/download.py", line 386, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 617, in send
history = [resp for resp in gen] if allow_redirects else []
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 617, in <listcomp>
history = [resp for resp in gen] if allow_redirects else []
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 177, in resolve_redirects
**adapter_kwargs
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/cachecontrol/adapter.py", line 47, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/adapters.py", line 497, in send
raise SSLError(e, request=request)
pip._vendor.requests.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.

发现是先下载了一个wget http://pypi.doubanio.com/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz,于是自己手动下载,解压看到

└── ssl-1.16
├── Makefile
├── PKG-INFO
├── README.rst
├── setup.py
├── ssl
└── test

打开setup.py,发现第33行就是第一个错误日志里面的错误代码,发现这个包只适合python2,尝试改了里面所有print语句的语法还是会报错。

     30     # Check the standard locations
31 for dir in std_dirs:
32 f = os.path.join(dir, filename)
33 print 'looking for', f
34 if os.path.exists(f):
35 return []

转移思路,以为是本机python2和python3同时存在导致,用pip下载的是python2版本的ssl包。于是百度怎么把python2卸载。于是看到了下面这篇文章

Centos7 python2.7和yum完全卸载及重装 https://www.cnblogs.com/wangjunjiehome/p/9239005.html

只看了完全卸载(卸载python和yum命令),没看重装部分。

参考的博文,注意rpm包的版本需要根据自己系统版本修改,譬如我的是CentOS Linux release 7.6.1810,rpm下载路径是http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/ (7.* 都是用7的rpm包)

1、删除现有Python
[root@test ~]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制删除已安装程序及其关联
[root@test ~]# whereis python |xargs rm -frv ##删除所有残余文件 ##xargs,允许你对输出执行其他某些命令
[root@test ~]# whereis python ##验证删除,返回无结果
2、删除现有的yum
[root@test ~]# rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps
[root@test ~]# whereis yum |xargs rm -frv ------------后面的部分需要根据自己本机报错去补充下载相应rpm包-------------------------- 3、从http://mirrors.ustc.edu.cn/centos/6.4/os/x86_64/Packages/下载相应的包
python-2.6.6-36.el6.x86_64.rpm
python-devel-2.6.6-36.el6.x86_64.rpm
python-libs-2.6.6-36.el6.x86_64.rpm
python-pycurl-7.19.0-8.el6.x86_64.rpm
python-setuptools-0.6.10-3.el6.noarch.rpm
python-urlgrabber-3.9.1-8.el6.noarch.rpm
python-iniparse-0.3.1-2.1.el6.noarch.rpm
rpm-python-4.8.0-32.el6.x86_64.rpm
yum-3.2.29-40.el6.centos.noarch.rpm
yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
yum-utils-1.1.30-14.el6.noarch.rpm
yum-plugin-fastestmirror-1.1.30-14.el6.noarch.rpm
yum-plugin-protectbase-1.1.30-14.el6.noarch.rpm
yum-plugin-aliases-1.1.30-14.el6.noarch.rpm
yum-plugin-downloadonly-1.1.30-14.el6.noarch.rpm
由于源中版本会更新,具体请查看URL中的版本再下载下来!
[root@test ~]# rpm -Uvh --replacepkgs python*.rpm
[root@test ~]# rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm
可能之间还需要zlib和zlib-devel包,根据情况下载并安装!

结果悲剧了,yum命令找不到了(好奇过为什么要把yum卸载,不知道linux的yum是依赖python的,不卸载yum是无法卸载python2的)。没有yum命令只用源码安装python2,缺少很多依赖,只能一个个手动下载rpm包,下面是我下载的所有rpm包

wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-build-libs-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-libs-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-build-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-cron-4.11.3-40.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-devel-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-plugin-systemd-inhibit-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-sign-4.11.3-40.el7.x86_64.rpm wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-2.7.5-86.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-devel-2.7.5-86.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-libs-2.7.5-86.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-pycurl-7.19.0-19.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-setuptools-0.9.8-7.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-urlgrabber-3.10-9.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-python-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-3.4.3-163.el7.centos.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-utils-1.1.31-52.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-52.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-plugin-protectbase-1.1.31-52.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-plugin-aliases-1.1.31-52.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-rpm-macros-3-32.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-backports-1.0-8.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python2-rpm-macros-3-32.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-ipaddress-1.0.16-2.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-srpm-macros-3-32.el7.noarch.rpm wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-kitchen-1.1.1-5.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-chardet-2.2.1-3.el7.noarch.rpm

安装rpm包:

rpm -Uvh --replacepkgs python*.rpm
rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm

我的yum命令终于可以用了!!!

最后解决python3的ssl模块问题,原来是要用pyopenssl,完美解决

pip install pyopenssl

重新编译python3

./Modules/readline.c:32:31: fatal error: readline/readline.h: No such file or directory

python3使用requests模块发https请求,提示caused by ssl error, can't connect to https url because the ssl module is not available的更多相关文章

  1. [实战演练]python3使用requests模块爬取页面内容

    本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...

  2. Python3之Requests模块详解

    # 导入 Request模块 # 若本机无自带Request模块,可自行下载或者使用pip进行安装 # python版本Python3 import requests import json #### ...

  3. Python3:Requests模块的异常值处理

    Python3:Requests模块的异常值处理 用Python的requests模块进行爬虫时,一个简单高效的模块就是requests模块,利用get()或者post()函数,发送请求. 但是在真正 ...

  4. Python接口测试-使用requests模块发送GET请求

    本篇主要记录下使用python的requests模块发送GET请求的实现代码. 向服务器发送get请求:无参数时:r = requests.get(url)带params时:r = requests. ...

  5. Python接口测试-使用requests模块发送post请求

    本篇主要记录下使用python的requests模块发送post请求的实现代码. #coding=utf-8 import unittest import requests class PostTes ...

  6. python3使用requests模块完成get/post/代理/自定义header/自定义Cookie

    一.背景说明 http请求的难易对一门语言来说是很重要的而且是越来越重要,但对于python一是urllib一些写法不太符合人的思维习惯文档也相当难看,二是在python2.x和python3.x中写 ...

  7. (转)Python3之requests模块

    原文:https://www.cnblogs.com/wang-yc/p/5623711.html Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另 ...

  8. Python3之requests模块

    Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务. 发送G ...

  9. python 爬虫 基于requests模块的get请求

    需求:爬取搜狗首页的页面数据 import requests # 1.指定url url = 'https://www.sogou.com/' # 2.发起get请求:get方法会返回请求成功的响应对 ...

  10. 基于python第三方requests 模块的HTTP请求类

    使用requests模块构造的下载器,首先安装第三方库requests pip install requests 1 class StrongDownload(object): def __init_ ...

随机推荐

  1. React综合使用联系

    index.js import React from 'react' import ReactDOM from 'react-dom' import CartSimple from './CartSi ...

  2. springboot接收前端传参的几种方式

    1.通过HttpServletRequest接收,常用于获取请求头参数以及Cookie,适用于GET 和 POST请求方式,以下两种方式: @GetMapping("/demo1" ...

  3. Azure DevOps Server 用户组加入 Azure AD Domain Service 管理用户

    一,引言 今天我们继续讲解 Azure DevOps Server 的内容,对于管理用户组除了在 Azure DevOps Server 服务器上添加管理员方式外,还有没有其他方式,Azure Dev ...

  4. 关于Module Not Found Error No module named Crypto解决

    前言 之前就遇到这个问题, 当然是windows上具有的问题 问题描述 from Crypto.Cipher import AES 出现 ModuleNotFoundError: No module ...

  5. Spring Security(8)

    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来- 之前虽然实现了角色和权限之间的简单配对,但是如果每一个角色都要重新来过一次,就有点呆板了.如果能够配置一个「角色模板」,再通过这个模板来配置其他 ...

  6. 深度剖析 | 【JVM深层系列】[HotSpotVM研究系列] JVM调优的"标准参数"的各种陷阱和坑点分析(攻克盲点及混淆点)「 1 」

    [易错问题]Major GC和Full GC的区别是什么?触发条件呢? 相信大多数人的理解是Major GC只针对老年代,Full GC会先触发一次Minor GC,不知对否?我参考了R大的分析和介绍 ...

  7. Python全栈工程师之从网页搭建入门到Flask全栈项目实战(7) - 在线问答系统

    1.项目源码/业务逻辑 百度网盘链接:链接:https://pan.baidu.com/s/13VNfrSJE6vcL3HP1J5T8ew 提取码:00s0,项目业务逻辑自行阅读 2.项目搭建 点击新 ...

  8. Django之SQL注入漏洞复现(CVE-2021-35042)

    前言 SQL注入的原理是对web请求,表单或域名等提交查询的字符串没有进行安全检测过滤,攻击者可以拼接执行恶意SQL命令,导致用户数据泄露 漏洞原理 Django 组件存在 SQL 注入漏洞,该漏洞是 ...

  9. 基于 Traefik 如何实现 path 末尾自动加斜杠?

    前言 Traefik 是一个现代的 HTTP 反向代理和负载均衡器,使部署微服务变得容易. Traefik 可以与现有的多种基础设施组件(Docker.Swarm 模式.Kubernetes.Mara ...

  10. prometheus-添加监控linux服务器

    1. prometheus-添加监控linux服务器 prometheus添加监控linux服务器 node_exporter:用于监控Linux系统的指标采集器. 常用指标: CPU 内存 硬盘 网 ...