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. 关于linux mint(nemo)添加右键菜单修改方法

    前言 其实在 linux mint 桌面上右键弹出的菜单,以及在资源管理器 nemo 中右键菜单,这些都是基于 nemo,进行的操作,所以更改右键菜单也就是更改nemo的配置文件 操作 在目录 /ho ...

  2. Zabbix技术分享——使用docker-compose快速部署zabbix监控系统

    前面文章有提到过使用docker来快速拉起一个zabbix监控系统(详见:如何使用docker快速部署一个zabbix监控系统),但是要一个个执行docker启动命令去将对应的容器启动.如果要配置参数 ...

  3. JAVA学到方法写了一个四则运算计算器,请教一下有什么需要改进的

    package method; /* * 四则运算计算器 * */ import java.util.Scanner; public class Demo07 { public static void ...

  4. Velero系列文章(四):使用Velero进行生产迁移实战

    概述 目的 通过 velero 工具, 实现以下整体目标: 特定 namespace 在B A两个集群间做迁移; 具体目标为: 在B A集群上创建 velero (包括 restic ) 备份 B集群 ...

  5. GO语言基础 为什么我要学习Golang以及GO语言入门普及

    作为网络安全初学者,会遇到采用Go语言开发的恶意样本.因此从今天开始从零讲解Golang编程语言,一方面是督促自己不断前行且学习新知识:另一方面是分享与读者,希望大家一起进步.这系列文章入门部分将参考 ...

  6. 模板层之标签 自定义过滤器及标签 模板的继承与导入 模型层之前期准备 ORM常用关键字

    目录 模板层之标签 if判断 for循环 自定义过滤器.标签及inclusion_tag(了解) 前期三步骤 自定义过滤器(最大只能接收两个参数) 自定义标签(参数没有限制) 自定义inclusion ...

  7. C#代码扫描工具Sonarqube + Win10+SqlServer2017

    在之前的公司, 看到有用过代码扫描工具, 扫描C#代码, 最近公司也有考虑做这个,于是我便独自研究了一下,这里给大家做个分享 网上找了很多资料, 主要有以下问题: 1. Sonarqube用的是 旧版 ...

  8. CentOS7升级Linux内核

    CentOS7升级Linux内核 什么是Linux内核 虽然时候使用 Linux 来表示整个操作系统,严格地说,Linux 只是个内核,而发行版的操作系统是一个完整功能的系统,它建立在内核之上,具有各 ...

  9. 【RocketMQ】负载均衡源码分析

    RocketMQ在集群模式下,同一个消费组内,一个消息队列同一时间只能分配给组内的某一个消费者,也就是一条消息只能被组内的一个消费者进行消费,为了合理的对消息队列进行分配,于是就有了负载均衡. 接下来 ...

  10. Linux 驱动像单片机一样读取一帧dmx512串口数据

    硬件全志R528 目标:实现Linux 读取一帧dmx512串口数据. 问题分析:因为串口数据量太大,帧与帧之间的间隔太小.通过Linux自带的读取函数方法无法获取到 帧头和帧尾,读取到的数据都是缓存 ...