python requests證書問題解決

這篇文章主要介紹了python requests證書問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

用requests包請求https的網站時,我們偶爾會遇到證書問題。也就是常見的SSLerror,遇到這種問題莫慌莫慌。

這裏沒有找到合適的網站去報SSL證書的錯誤,所以就假裝請求了一個https的網站,然後給報了SSLerror了,然後下面是解決方法

可以直接關閉驗證ssl證書

import requests
'''
  :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
  :param verify: (optional) Either a boolean, in which case it controls whether we verify
      the server's TLS certificate, or a string, in which case it must be a path
      to a CA bundle to use. Defaults to ``True``.
      
'''
r = requests.get('https://kyfw.12306.cn',verify=False)

print(r.text)

這種方式直接在函數裏面加如verify改變Ture或者False即可,因爲post與get調用的都爲request()函數,所以get與post都一樣。

如果這種方式奏效就用這種方式,如果不奏效就用下面的一種

import requests
'''
  :param verify: (optional) Either a boolean, in which case it controls whether we verify
      the server's TLS certificate, or a string, in which case it must be a path
      to a CA bundle to use. Defaults to ``True``.
      
'''
## 證書路徑
cert = '../cert/test.pem'

r = requests.get('https://kyfw.12306.cn',verify=cert)
print(r.text)

就用這種,直接把證書的路徑丟給verify,請求即可

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章