python requests module ssl

Python Requests Module SSL

If you are working with web applications, you might come across a situation where you need to send HTTP requests over HTTPS. Python Requests module is a popular library that can be used to send HTTP requests easily. However, when you are sending requests over HTTPS, you need to ensure that the SSL certificate is valid and trusted.

Python Requests module provides support for SSL by default. When you make a request to an HTTPS endpoint, Requests module automatically verifies the SSL certificate. If the certificate is invalid or not trusted, it will raise an exception.

To disable SSL verification, you can set the verify parameter to False in the request. However, this is not recommended as it can make your application vulnerable to security attacks.

Using Requests Module with SSL


import requests
response = requests.get('https://example.com')
print(response.status_code)
  
  • import requests: Import the requests module
  • response = requests.get('https://example.com'): Send a GET request to an HTTPS endpoint
  • print(response.status_code): Print the status code of the response

Verifying SSL Certificates

By default, Requests module verifies SSL certificates when making HTTPS requests. This means that if the certificate is invalid or not trusted, Requests will raise a SSLError exception. You can verify the SSL certificate manually by specifying the path of the CA bundle file.


import requests
response = requests.get('https://example.com', verify='/path/to/certfile')
print(response.status_code)
  
  • verify='/path/to/certfile': Specify the path of the CA bundle file to use for SSL verification

Disabling SSL Verification

Disabling SSL verification is not recommended as it can make your application vulnerable to security attacks. However, if you still want to disable SSL verification, you can set the verify parameter to False in the request.


import requests
response = requests.get('https://example.com', verify=False)
print(response.status_code)
  
  • verify=False: Disable SSL verification