1. 程式人生 > 實用技巧 >Python介面自動化(四) https請求(SSLError解決辦法)

Python介面自動化(四) https請求(SSLError解決辦法)

電腦環境

python:3.7

requests:2.22.0

fiddler:v5.0.20

一、SSL問題

1.在你不啟用fiddler時,python程式碼直接傳送https請求,不會有SSL問題(也就是說不想看到SSL問題,關掉fiddler就行)

2.啟動fiddler抓包,會出現SSLError這個錯誤:

requests.exceptions.SSLError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))

解決辦法:

一、verify引數設定

  1.Requests的請求預設verify=True

  2.如果你將verify設定為False,Requests也能忽略對SSL證書的驗證

  3.但是依然會出現兩行Warning,可以不用管

二、忽略Warning

Python3新增如下三種任意一種程式碼即可解決;

import requests
import warnings
#warnings.filterwarnings("ignore") #方式1
#requests.packages.urllib3.disable_warnings()#方式2
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)#方式3
r=requests.get("https://www.baidu.com",verify=False)
print(r.status_code)