python 安装 requests
用户指南
http://cn.python-requests.org/zh_CN/latest/
1. 下载 requests 源码
因为工作电脑访问不到 github ,所以先通过能上网的电脑把源码下载下来,再 copy 到工作电脑。
a. 访问 https://github.com/kennethreitz/requests
b. 点击右下角的“Download ZIP”
c. 通过 U 盘 copy 到工作电脑
2. 安装
unzip requests-master.zip
mv requests-master requests
cd requests
python setup.py install
3. 验证
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>
解决 InsecurePlatformWarning
异常现象:
在 https 使用中提示 warning ,如下。
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import requests
>>>
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
/usr/lib/python2.6/site-packages/requests/packages/urllib3/util/ssl_.py:90: 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.
InsecurePlatformWarning
>>>
方法一:升级 python 版本
使用 2.7.9及以上版本可以规避此问题,我在 2.7.10 中使用 requests 正常。
Python 2.7.10 (default, Oct 4 2015, 17:50:54)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import requests
>>>
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
401
>>>
后面发现在 2.7.10 会报“urllib3/connectionpool.py:789: InsecureRequestWarning”这种 warning,方法二可以避免 warning。
Python 2.7.10 (default, Oct 8 2015, 17:29:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import requests
>>> r = requests.get('https://10.25.7.2',verify=False)
/usr/local/python27/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py:789: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)
>>> r.status_code
200
>>> requests.packages.urllib3.disable_warnings()
>>> r = requests.get('https://10.25.7.2',verify=False)
>>>
>>> r.status_code
200
>>>
在 Python 3.5.0 中使用 requests 正常
[root@payun Python-3.5.0]# pip35 install requests
Collecting requests
Downloading requests-2.7.0-py2.py3-none-any.whl (470kB)
100% |████████████████████████████████| 471kB 702kB/s
Installing collected packages: requests
Successfully installed requests-2.7.0
[root@payun Python-3.5.0]#
[root@payun Python-3.5.0]# python35
Python 3.5.0 (default, Oct 6 2015, 10:17:03)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import requests
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>>
>>> r.status_code
401
>>>
>>>
方法二:disable warnings
可以通过 disable_warnings 方法关闭 warning
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import requests
>>>
>>> r = requests.get('https://10.25.7.2',verify=False)
/usr/lib/python2.6/site-packages/requests/packages/urllib3/util/ssl_.py:90: 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.
InsecurePlatformWarning
/usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)
>>>
>>> requests.packages.urllib3.disable_warnings()
>>>
>>> r = requests.get('https://10.25.7.2',verify=False)
>>>
如下是 urllib3 文档的说明
InsecurePlatformWarning
New in version 1.11.
Certain Python platforms (specifically, versions of Python earlier than 2.7.9) have restrictions in their ssl module that limit the configuration that urllib3 can apply. In particular, this can cause HTTPS requests that would succeed on more featureful platforms to fail, and can cause certain security features to be unavailable.
If you encounter this warning, it is strongly recommended you upgrade to a newer Python version, or that you use pyOpenSSL as described in theOpenSSL / PyOpenSSL section.
For info about disabling warnings, see Disabling Warnings.
参考
http://stackoverflow.com/questions/29099404/ssl-insecureplatform-error-when-using-requests-package