我使用Python 2.7.3和请求。我通过pip安装了Requests。我相信这是最新的版本。我在Debian Wheezy上运行。
我在过去使用过很多次请求,从来没有遇到过这个问题,但似乎当使用请求进行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安装请求[安全]
或
pip install pyOpenSSL ndg-httpsclient pyasn1
这两个命令都安装以下额外的包:
pyOpenSSL
密码学
idna
请注意,python-2.7.9+不是必需的。
如果pip安装失败并出现错误,请使用发行版的包管理器检查是否在系统中安装了libffi、libssl和python所需的开发包:
Debian/Ubuntu - python-dev libffi-dev libssl-dev包。
Fedora - openssl-devel python-devel libffi-devel包。
上面的发行版列表不完整。
解决方案(参见@TomDotTom的原始答案):
如果你不能安装一些必要的开发包,还有一个选项可以禁用该警告:
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
如果您的pip本身受到InsecurePlatformWarning的影响,并且无法从PyPI安装任何东西,可以通过以下逐步指南手动部署额外的python包来修复。
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李勃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.
...
"""
这个答案是不相关的,但如果你想摆脱警告,并从请求中得到以下警告:
InsecurePlatformWarning
/usr/local/lib/python2.7/ disti -packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: true SSLContext对象不可用。这将阻止urllib3正确配置SSL,并可能导致某些SSL连接失败。欲了解更多信息,请参见https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning。
您可以通过在python代码中添加以下行来禁用它:
requests.packages.urllib3.disable_warnings ()