python requests library disable ssl

Python Requests Library Disable SSL

If you are using Python requests library to make HTTP requests to an SSL secured website, you might encounter an error related to SSL certificate verification. In some cases, the SSL certificate may not be trusted or you may want to disable SSL verification for testing purposes. In such scenarios, you can disable SSL verification using the requests library.

Method 1: Disabling SSL Verification Globally

You can disable SSL verification globally for all requests using the following code:


import requests
requests.packages.urllib3.disable_warnings() # Disable SSL verification globally

response = requests.get("https://example.com")
print(response.content)
  

The above code disables SSL verification globally for all requests made using the requests library. This is not recommended in production environments as it poses a security risk.

Method 2: Disabling SSL Verification for a Single Request

You can disable SSL verification for a single request using the verify parameter set to False:


import requests

response = requests.get("https://example.com", verify=False)
print(response.content)
  

The above code disables SSL verification for the request made to https://example.com. This is a better approach than disabling SSL globally because it limits the exposure to security risks to a single request.

Method 3: Disabling SSL Verification for a Specific Certificate

If you encounter an SSL certificate error due to an untrusted certificate, you can verify the certificate manually and disable SSL verification for that specific certificate. You can do this by passing the path to the certificate file to the verify parameter:


import requests

response = requests.get("https://example.com", verify="/path/to/certificate.pem")
print(response.content)
  

The above code verifies the SSL certificate using the certificate file located at /path/to/certificate.pem. This is a better approach than disabling SSL globally because it limits the exposure to security risks to a specific certificate.