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. 聊一聊如何截获 C# 程序产生的日志

    一:背景 1.讲故事 前段时间分析了一个dump,一顿操作之后,我希望用外力来阻止程序内部对某一个com组件的调用,对,就是想借助外力实现,如果用 windbg 的话,可以说非常轻松,但现实情况比较复 ...

  2. linux sublime-text ctrl+shift+b 快捷键失效问题解决

    解决办法 由于fcitx拦截了这个ctrl+shift+b 这个快捷键,所以取消即可 点击全局配置里面高级选项,然后找到ctrl+shift+b这个快捷键,点击后,按esc就可以将快捷键设置为空,不过 ...

  3. 数据结构初阶--堆排序+TOPK问题

    堆排序 堆排序的前提 堆排序:是指利用堆这种数据结构所设计的一种排序算法.堆排序通过建大堆或者小堆来进行排序的算法. 举个例子:给定我们一个数组{2, 3,4, 2,4,7},我们可把这个数组在逻辑上 ...

  4. python-名称空间与作用域

    名称空间与作用域 (1)名称空间(namespaces) 用来存放名字的地方是对栈区的划分 有了名称空间,就可在栈区中放相同的名字 1.1.内置名称空间 存放的名字:是Python解释器内置的名字 & ...

  5. 外部引入css样式报错Resource interpreted as Stylesheet but transferred with MIME type html/text

    Resource interpreted as Stylesheet but transferred with MIME type html/text 解决方法: 1.将content-type改为t ...

  6. WeetCode3 暴力递归->记忆化搜索->动态规划

    笔者这里总结的是一种套路,这种套路笔者最先是从左程云的b站视频学习到的 本文进行简单总结 系列文章目录和关于我 一丶动态规划的思想 使用dp数组记录之前状态计算的最佳结果,找出当前状态和之前状态的关系 ...

  7. 自研分布式高性能RPC框架及服务注册中心实践笔记【原创】【开源】

    痛点 1. bsf底层依赖springcloud,影响bsf更新springboot新版本和整体最新技术版本升级. 2. eureka已经闭源,且框架设计较重,同时引入eureka会自行引入较多spr ...

  8. 运行typhoon程序的三种方式

    cmd直接编写运行:用于较短 临时执行的代码 解释器命令运行:可以编写较长的代码 并且可以长久保存 利用IDE工具编写:IDE开发者工具自动提示 携带各种功能插件 编写代码效率更高更快

  9. 常用内置模块os sys json

    今日内容回顾 目录 今日内容回顾 os模块 sys模块 json模块 json模块实战 os模块 sys模块 json模块 os模块 os模块主要与代码运行的操作系统打交道 1.创建目录(文件夹) i ...

  10. 利用WordPress搭建属于自己的网站

    怎么用WordPress给自己搭建了一个网站?可能很多人都想拥有属于自己的网站,这篇文章就找你怎么利用WordPress搭建属于自己的网站.如果你也正好有搭建个人网站的想法,那么本文会给你一个参考,我 ...