Python request設定HTTPS代理程式碼解析

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

之前版本的代理中,可以使用fiddler進行HTTP包的代理,但是代理HTTPS包時,執行錯誤


self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

urllib或requests在開啟https站點是會驗證證書,簡單的處理方式就是把傳送https請求時把驗證ssl證書關掉,即設定verify為False

具體程式碼設定如:


requests.post(host, data=requestData, headers=headerData,proxies=proxies,verify=False)

取消SSL驗證又帶來一個新問題

C:\Users\lluozh\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py:852: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)

雖然不是錯誤,但是在捕捉異常時,預設會出問題,為了不影響原有流程,應該去掉這些警告資訊


from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

或者直接取消所有urllib3的警告


requests.packages.urllib3.disable_warnings()

總結

相關文章

程式語言 最新文章