- Requests 教程
- Requests - 主頁
- Requests - 概述
- Requests - 環境設定
- Requests - HTTP 請求如何工作?
- Requests - 使用 Requests
- 處理 HTTP 請求的響應
- Requests - HTTP 請求頭
- Requests - 處理 GET 請求
- 處理 POST、PUT、PATCH 和 DELETE 請求
- Requests - 檔案上傳
- Requests - 使用 Cookie
- Requests - 處理錯誤
- Requests - 處理超時
- Requests - 處理重定向
- Requests - 處理歷史記錄
- Requests - 處理會話
- Requests - SSL 證書
- Requests - 身份驗證
- Requests - 事件鉤子
- Requests - 代理
- Requests - 使用 Requests 進行網路抓取
- Requests 有用資源
- Requests - 快速指南
- Requests - 有用資源
- Requests - 討論
Requests - SSL 證書
SSL 證書是一種隨安全 URL 提供的安全特性。當你使用 Requests 庫時,它還會驗證給定的 https URL 的 SSL 證書。SSL 驗證在請求模組中預設啟用,並且在沒有此證書時會引發錯誤。
使用安全 URL
以下是使用安全 URL 的示例 −
import requests getdata = requests.get(https://jsonplaceholder.typicode.com/users) print(getdata.text)
輸出
E:\prequests>python makeRequest.py
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
]
我們能輕鬆地從上述 https URL 獲得響應,這是因為請求模組能夠驗證 SSL 證書。
只需新增 verify=False,你就可以停用 SSL 驗證,如下例所示。
示例
import requests
getdata =
requests.get('https://jsonplaceholder.typicode.com/users', verify=False)
print(getdata.text)
你會得到輸出,但它也會顯示一條警告訊息,即 SSL 證書未經驗證,建議新增證書驗證。
輸出
E:\prequests>python makeRequest.py connectionpool.py:851: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3 .readthedocs.io/en/latest/advanced-usage.htm l#ssl-warnings InsecureRequestWarning) [ { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496" } }, "phone": "1-770-736-8031 x56442", "website": "hildegard.org", "company": { "name": "Romaguera-Crona", "catchPhrase": "Multi-layered client-server neural-net", "bs": "harness real-time e-markets" } } ]
你還可以透過在你的終端託管 SSL 證書,然後使用 verify 引數指定路徑,如下所示,驗證 SSL 證書。
示例
import requests
getdata =
requests.get('https://jsonplaceholder.typicode.com/users', verify='C:\Users\AppData\Local\certificate.txt')
print(getdata.text)
輸出
E:\prequests>python makeRequest.py
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
]
廣告