使用请求包时出现 SSL InsecurePlatform 错误
SSL InsecurePlatform error when using Requests package
我正在使用 Python 2.7.3 和请求。我通过 pip 安装了 Requests。我相信这是最新版本。我 运行 使用 Debian Wheezy。
我过去曾多次使用 Requests,但从未遇到过这个问题,但似乎在使用 Requests
发出 https 请求时出现 InsecurePlatform
异常。
错误提到 urllib3
,但我没有安装它。我确实安装了它来检查它是否解决了错误,但没有。
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3
/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not
available. This prevents urllib3 from configuring SSL appropriately and
may cause certain SSL connections to fail. For more information, see
https://urllib3.readthedocs.org/en/latest
/security.html#insecureplatformwarning.
关于我为什么收到这个的任何想法?我已经按照错误消息中的说明检查了文档,但是文档说要导入 urllib3 并禁用警告或提供证书。
使用稍微隐藏的安全功能:
pip install requests[security]
要么
pip install pyOpenSSL ndg-httpsclient pyasn1
这两个命令都会安装以下额外的软件包:
- pyOpenSSL
- 密码学
- idna
请注意,python-2.7.9+ 不需要这样做。
如果 pip install
因错误而失败,请检查您的系统中是否安装了 libffi
、libssl
和 python
所需的开发包 使用发行版的包管理器:
Debian/Ubuntu - python-dev
libffi-dev
libssl-dev
包。
Fedora - openssl-devel
python-devel
libffi-devel
包。
上面的发行版列表不完整。
解决方法 ():
如果您无法安装某些必需的开发包,还有一个选项可以禁用该警告:
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
如果您的 pip
本身受到 InsecurePlatformWarning
的影响并且无法从 PyPI 安装任何东西,可以使用 this step-by-step guide 修复它以手动部署额外的 python 包。
如果您无法升级您的 Python 版本到 2.7.9,并且想要抑制警告,
您可以将您的 'requests' 版本降级到 2.5.3:
sudo pip install requests==2.5.3
关于版本:
http://fossies.org/diffs/requests/2.5.3_vs_2.6.0/requests/packages/urllib3/util/ssl_.py-diff.html
Requests 2.6 为 2.7.9 之前的 python 用户引入了此警告,只有可用的库存 SSL 模块。
假设您无法升级到更新版本的 python,这将安装更多最新的 python SSL 库:
pip install --upgrade ndg-httpsclient
但是,这在某些没有 pyOpenSSL 构建依赖项的系统上可能会失败。在 debian 系统上,运行 上面的 pip 命令之前的这个应该足以让 pyOpenSSL 构建:
apt-get install python-dev libffi-dev libssl-dev
其实你可以试试这个
requests.post("https://www.google.com", verify=False)
您可以阅读请求代码。
"C:\Python27\Lib\site-packages\requests\sessions.py"
class Session(SessionRedirectMixin):
......
def request(self, method, url,
params=None,
data=None,
headers=None,
cookies=None,
files=None,
auth=None,
timeout=None,
allow_redirects=True,
proxies=None,
hooks=None,
stream=None,
verify=None, # <========
cert=None):
"""
...
:param verify: (optional) if True, the SSL cert will be verified.
A CA_BUNDLE path can also be provided.
...
"""
我不在生产中使用它,只是一些测试运行器。并重申 urllib3 documentation
If you know what you are doing and would like to disable this and
other warnings
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
编辑/更新:
以下也应该有效:
import logging
import requests
# turn down requests log verbosity
logging.getLogger('requests').setLevel(logging.CRITICAL)
此答案无关,但如果您想摆脱警告并从请求中获取以下警告:
InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
您可以通过在 python 代码中添加以下行来 禁用 它:
requests.packages.urllib3.disable_warnings()
对我来说没有工作我需要升级 pip....
Debian/Ubuntu
安装依赖项
sudo apt-get install libpython-dev libssl-dev libffi-dev
升级 pip 并安装包
sudo pip install -U pip
sudo pip install -U pyopenssl ndg-httpsclient pyasn1
如果你想删除依赖项
sudo apt-get remove --purge libpython-dev libssl-dev libffi-dev
sudo apt-get autoremove
我刚刚在 CentOS 5 服务器上遇到了类似的问题,我在 /usr/local 中安装了 python 2.7.12,这是一个更旧版本的 python2.7。目前无法在此服务器上升级到 CentOS 6 或 7。
某些 python 2.7 模块仍然存在于旧版本的 python,但 pip 无法升级,因为 CentOS 5 组件不支持较新的加密组件。
具体来说,'pip install requests[security]' 失败是因为 CentOS 5 上的 openssl 版本是 0.9.8e,密码学 > 1.4.0 不再支持它。
为了解决 OP 的原始问题,我做了:
1) pip install 'cryptography<1.3.5,>1.3.0'.
这安装了与 openssl-0.9.8e 一起使用的密码学 1.3.4。 cryptography 1.3.4 也足以满足以下命令的要求。
2) pip install 'requests[security]'
此命令现在可以安装,因为它不会尝试安装 > 1.4.0 的密码系统。
请注意,在 Centos 5 上我还需要:
yum install openssl-devel
允许加密构建
这里给出的所有解决方案都没有帮助(我只能使用 python 2.6.6)。我在传递给 pip 的简单开关中找到了答案:
$ sudo pip install --trusted-host pypi.python.org <module_name>
这告诉 pip 可以从 pypi.python.org.
抓取模块
对我来说,问题是我公司在其防火墙后面的代理使其看起来像某些服务器的恶意客户端。安全万岁。
更新:见@Alex 的
用于 PyPi 域中的更改,以及可以添加的其他 --trusted-host
选项。 (我会 copy/paste 这里,但他的回答,所以 +1 他)
我必须先去 bash
(来自 ZSH)。然后
sudo -H pip install 'requests[security]' --upgrade
已解决问题。
以下是我在 Python 3.6 上的工作方式:
import requests
import urllib3
# Suppress InsecureRequestWarning: Unverified HTTPS
urllib3.disable_warnings()
不要安装 pyOpenSSL,因为它很快就会被弃用。目前最好的方法是-
import requests
requests.packages.urllib3.disable_warnings()
上周我在 Ubuntu 14.04(使用 Python 2.7.6)做了一个 apt-get dist-upgrade
,其中包括来自 [=14] 的 libssl1.1:amd64
=].
因为我 运行 certbot-auto renew
从 cron 工作,我也使用 --no-self-upgrade
来减少计划外维护。这似乎是麻烦的根源。
要修复错误,我需要做的就是成为 root 用户(使用 su
的 --login
开关)并让 certbot-auto
自行升级。即:
sudo su --login
/usr/local/bin/certbot-auto renew
# ... Upgrading certbot-auto 0.8.1 to 0.18.2... blah blah blah ...
而不是通常 运行 来自 root 的 crontab 的内容:
5 7 * * * /usr/local/bin/certbot-auto renew --quiet --no-self-upgrade
之后,让我们再次正常加密 renwals 运行。
如果您只想停止不安全的警告,例如:
/usr/lib/python3/dist-packages/urllib3/connectionpool.py:794:
InsecureRequestWarning: Unverified HTTPS request is being made. Adding
certificate verification is strongly advised. See:
https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)
做:
requests.METHOD("https://www.google.com", verify=False)
verify=False
是关键,以下不擅长:
requests.packages.urllib3.disable_warnings()
或
urllib3.disable_warnings()
但是,您必须知道,这可能会导致潜在的安全风险。
我和
有同样的问题
Mac
Pycharm 社区版 2019.3
Python 解释器 3.6。
使用 20.0.2 升级 pip 对我有用。
Pycharm --> Preferences --> Project Interpreter --> click on pip --> specify version 20.0.2 --> Install package
在我的例子中,我正在处理一个旧的 ubuntu 可信图像并尝试安装 python dateutil。我必须首先使用以下命令将 python 升级到 2.7.12:
add-apt-repository -y ppa:fkrull/deadsnakes-python2.7
apt-get -y update
apt install -y --force-yes python2.7-minimal
pip install python-dateutil
我正在使用 Python 2.7.3 和请求。我通过 pip 安装了 Requests。我相信这是最新版本。我 运行 使用 Debian Wheezy。
我过去曾多次使用 Requests,但从未遇到过这个问题,但似乎在使用 Requests
发出 https 请求时出现 InsecurePlatform
异常。
错误提到 urllib3
,但我没有安装它。我确实安装了它来检查它是否解决了错误,但没有。
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3
/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not
available. This prevents urllib3 from configuring SSL appropriately and
may cause certain SSL connections to fail. For more information, see
https://urllib3.readthedocs.org/en/latest
/security.html#insecureplatformwarning.
关于我为什么收到这个的任何想法?我已经按照错误消息中的说明检查了文档,但是文档说要导入 urllib3 并禁用警告或提供证书。
使用稍微隐藏的安全功能:
pip install requests[security]
要么
pip install pyOpenSSL ndg-httpsclient pyasn1
这两个命令都会安装以下额外的软件包:
- pyOpenSSL
- 密码学
- idna
请注意,python-2.7.9+ 不需要这样做。
如果 pip install
因错误而失败,请检查您的系统中是否安装了 libffi
、libssl
和 python
所需的开发包 使用发行版的包管理器:
Debian/Ubuntu -
python-dev
libffi-dev
libssl-dev
包。Fedora -
openssl-devel
python-devel
libffi-devel
包。
上面的发行版列表不完整。
解决方法 (
如果您无法安装某些必需的开发包,还有一个选项可以禁用该警告:
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
如果您的 pip
本身受到 InsecurePlatformWarning
的影响并且无法从 PyPI 安装任何东西,可以使用 this step-by-step guide 修复它以手动部署额外的 python 包。
如果您无法升级您的 Python 版本到 2.7.9,并且想要抑制警告,
您可以将您的 'requests' 版本降级到 2.5.3:
sudo pip install requests==2.5.3
关于版本: http://fossies.org/diffs/requests/2.5.3_vs_2.6.0/requests/packages/urllib3/util/ssl_.py-diff.html
Requests 2.6 为 2.7.9 之前的 python 用户引入了此警告,只有可用的库存 SSL 模块。
假设您无法升级到更新版本的 python,这将安装更多最新的 python SSL 库:
pip install --upgrade ndg-httpsclient
但是,这在某些没有 pyOpenSSL 构建依赖项的系统上可能会失败。在 debian 系统上,运行 上面的 pip 命令之前的这个应该足以让 pyOpenSSL 构建:
apt-get install python-dev libffi-dev libssl-dev
其实你可以试试这个
requests.post("https://www.google.com", verify=False)
您可以阅读请求代码。
"C:\Python27\Lib\site-packages\requests\sessions.py"
class Session(SessionRedirectMixin):
......
def request(self, method, url,
params=None,
data=None,
headers=None,
cookies=None,
files=None,
auth=None,
timeout=None,
allow_redirects=True,
proxies=None,
hooks=None,
stream=None,
verify=None, # <========
cert=None):
"""
...
:param verify: (optional) if True, the SSL cert will be verified.
A CA_BUNDLE path can also be provided.
...
"""
我不在生产中使用它,只是一些测试运行器。并重申 urllib3 documentation
If you know what you are doing and would like to disable this and other warnings
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
编辑/更新:
以下也应该有效:
import logging
import requests
# turn down requests log verbosity
logging.getLogger('requests').setLevel(logging.CRITICAL)
此答案无关,但如果您想摆脱警告并从请求中获取以下警告:
InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
您可以通过在 python 代码中添加以下行来 禁用 它:
requests.packages.urllib3.disable_warnings()
对我来说没有工作我需要升级 pip....
Debian/Ubuntu
安装依赖项
sudo apt-get install libpython-dev libssl-dev libffi-dev
升级 pip 并安装包
sudo pip install -U pip
sudo pip install -U pyopenssl ndg-httpsclient pyasn1
如果你想删除依赖项
sudo apt-get remove --purge libpython-dev libssl-dev libffi-dev
sudo apt-get autoremove
我刚刚在 CentOS 5 服务器上遇到了类似的问题,我在 /usr/local 中安装了 python 2.7.12,这是一个更旧版本的 python2.7。目前无法在此服务器上升级到 CentOS 6 或 7。
某些 python 2.7 模块仍然存在于旧版本的 python,但 pip 无法升级,因为 CentOS 5 组件不支持较新的加密组件。
具体来说,'pip install requests[security]' 失败是因为 CentOS 5 上的 openssl 版本是 0.9.8e,密码学 > 1.4.0 不再支持它。
为了解决 OP 的原始问题,我做了:
1) pip install 'cryptography<1.3.5,>1.3.0'.
这安装了与 openssl-0.9.8e 一起使用的密码学 1.3.4。 cryptography 1.3.4 也足以满足以下命令的要求。
2) pip install 'requests[security]'
此命令现在可以安装,因为它不会尝试安装 > 1.4.0 的密码系统。
请注意,在 Centos 5 上我还需要:
yum install openssl-devel
允许加密构建
这里给出的所有解决方案都没有帮助(我只能使用 python 2.6.6)。我在传递给 pip 的简单开关中找到了答案:
$ sudo pip install --trusted-host pypi.python.org <module_name>
这告诉 pip 可以从 pypi.python.org.
抓取模块对我来说,问题是我公司在其防火墙后面的代理使其看起来像某些服务器的恶意客户端。安全万岁。
更新:见@Alex 的
--trusted-host
选项。 (我会 copy/paste 这里,但他的回答,所以 +1 他)
我必须先去 bash
(来自 ZSH)。然后
sudo -H pip install 'requests[security]' --upgrade
已解决问题。
以下是我在 Python 3.6 上的工作方式:
import requests
import urllib3
# Suppress InsecureRequestWarning: Unverified HTTPS
urllib3.disable_warnings()
不要安装 pyOpenSSL,因为它很快就会被弃用。目前最好的方法是-
import requests
requests.packages.urllib3.disable_warnings()
上周我在 Ubuntu 14.04(使用 Python 2.7.6)做了一个 apt-get dist-upgrade
,其中包括来自 [=14] 的 libssl1.1:amd64
=].
因为我 运行 certbot-auto renew
从 cron 工作,我也使用 --no-self-upgrade
来减少计划外维护。这似乎是麻烦的根源。
要修复错误,我需要做的就是成为 root 用户(使用 su
的 --login
开关)并让 certbot-auto
自行升级。即:
sudo su --login
/usr/local/bin/certbot-auto renew
# ... Upgrading certbot-auto 0.8.1 to 0.18.2... blah blah blah ...
而不是通常 运行 来自 root 的 crontab 的内容:
5 7 * * * /usr/local/bin/certbot-auto renew --quiet --no-self-upgrade
之后,让我们再次正常加密 renwals 运行。
如果您只想停止不安全的警告,例如:
/usr/lib/python3/dist-packages/urllib3/connectionpool.py:794: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html InsecureRequestWarning)
做:
requests.METHOD("https://www.google.com", verify=False)
verify=False
是关键,以下不擅长:
requests.packages.urllib3.disable_warnings()
或
urllib3.disable_warnings()
但是,您必须知道,这可能会导致潜在的安全风险。
我和
有同样的问题
Mac
Pycharm 社区版 2019.3
Python 解释器 3.6。
使用 20.0.2 升级 pip 对我有用。
Pycharm --> Preferences --> Project Interpreter --> click on pip --> specify version 20.0.2 --> Install package
在我的例子中,我正在处理一个旧的 ubuntu 可信图像并尝试安装 python dateutil。我必须首先使用以下命令将 python 升级到 2.7.12:
add-apt-repository -y ppa:fkrull/deadsnakes-python2.7
apt-get -y update
apt install -y --force-yes python2.7-minimal
pip install python-dateutil