Requests 简明教程

Requests - SSL Certification

SSL证书是安全网址附带的一项安全功能。当您使用 Requests 库时,它也会验证给定 https 网址的 SSL 证书。SSL 验证在 requests 模块中默认启用,如果证书不存在,则会引发错误。

SSL certificate is a security feature that comes with secure urls. When you use Requests library, it also verifies SSL certificates for the https URL given. SSL verification is enabled by default in the requests module and will throw an error if the certificate is not present.

Working with secure URL

以下是使用安全 URL 的示例 −

Following is the example of working with secure URL −

import requests
getdata = requests.get(https://jsonplaceholder.typicode.com/users)
print(getdata.text)

Output

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 网址获取响应,这是因为 request 模块可以验证 SSL 证书。

We are easily getting a response from the above https URL, and it is because the request module can verify the SSL certificate.

您可以通过在示例中所示的方式简单添加 verify=False 来禁用 SSL 验证。

You can disable the SSL verification by simply adding verify=False as shown in the example below.

Example

import requests
getdata =
requests.get('https://jsonplaceholder.typicode.com/users', verify=False)
print(getdata.text)

您将获得输出,但它还会给出一条警告消息,即 SSL 证书尚未得到验证,建议添加证书验证。

You will get the output, but it will also give a warning message that, the SSL certificate is not verified and adding certificate verification is advised.

Output

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 证书,如下所示。

You can also verify SSL certificate by hosting it at your end, and giving the path using verify param as shown below.

Example

import requests
getdata =
requests.get('https://jsonplaceholder.typicode.com/users', verify='C:\Users\AppData\Local\certificate.txt')
print(getdata.text)

Output

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"
      }
   }
]