Requests - SSL Certification



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

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

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

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)

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

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