Python - requests https请求的坑

Python - requests https请求的坑

#-*-coding:utf-8-*-
# Time:2017/9/25 20:41
# Author:YangYangJun import requests
import ssl
from requests.auth import HTTPBasicAuth def post_login(): url = "https://www.url"
values = {}
values['username'] = 'username'
values['password'] = 'password'
response = requests.post(url,values)
print response.text def get_login():
url = "https://www.url"
values = {}
values['username'] = 'username'
values['password'] = 'password'
#geturl = url + '?' + values
response = requests.get(url, values)
print response.content def post_loginHttps1(): url = "https://www.url"
values = {}
values['username'] = 'username'
values['password'] = 'password'
#临时解决https的方法1
response = requests.post(url,values,verify=False)
print response.text def post_loginHttps2():
#解决https方法2
ssl._create_default_https_context = ssl._create_unverified_context url = "https://www.url"
values = {}
values['username'] = 'username'
values['password'] = 'password'
#临时解决https的问题
response = requests.post(url,values,verify=True)
print response.text if __name__ == '__main__':
post_login()
#get_login()
post_loginHttps1()
# post_loginHttps2() #出现下面错误的原因主要是因为打开了fiddler,关闭fiddler即可。 # raise SSLError(e, request=request)
# requests.exceptions.SSLError: HTTPSConnectionPool(host='www.yiyao.cc', port=443): Max retries exceeded with url: /user/loginWeb (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)'),))
#

知乎:https://www.zhihu.com/question/40025043  有介绍解决的办法,如上代码,方法一:关闭校验,方法二:导入其他库,定义ssl._create_default_https_context = ssl._create_unverified_context。

其实根本原因是打开了fiddler,关闭即可,如果不关闭,方法一也可以,方法二还是不好使。故执行时要关闭 fiddler是最保险的。

同时如果打开了fiddler,访问https网站时,页面可能会提示不信任无法加载的提示,这是fiddler证书不受信任的问题。解决办法如下。

Python - requests https请求的坑

Python - requests https请求的坑

然后选择export root .....

然后桌面会生成一个

Python - requests https请求的坑

然后通过 不同浏览器的证书导入即可

Python - requests https请求的坑

选择证书执行导入。

Python - requests https请求的坑

上一篇:自然语言15_Part of Speech Tagging with NLTK


下一篇:R树空间索引及其变种