python requests library add certificate

How to Add Certificate to Python Requests Library

If you are working with Python Requests Library and trying to make a request to a website that requires SSL/TLS certificates, you may encounter an SSL error. To resolve this, you can add the certificate to the Requests Library.

Method 1: Add Certificate using verify Parameter

This method involves passing the path of the certificate file to the verify parameter of the requests.get() method. Here is how to do it:


import requests

cert_path = '/path/to/certfile.pem'

response = requests.get('https://example.com', verify=cert_path)

print(response.text)

In the above code, we import the Requests Library and set the path of our certificate file to 'cert_path'. We then make a GET request to 'https://example.com' and pass our certificate path to the verify parameter.

Method 2: Add Certificate using verify Parameter and Bundle

This method involves creating a certificate bundle file that contains your certificate and passing the path of this file to the verify parameter of the requests.get() method. Here is how to do it:

  1. First, open a text editor and paste your certificate in PEM format.
  2. Save the file as 'mycert.pem'.
  3. Next, open a new text editor window and paste the contents of your certificate bundle file.
  4. Save the file as 'mycerts.pem' (or any name you like).

Now that you have your certificate and certificate bundle files, you can use them to make a request with Requests Library:


import requests

cert_bundle_path = '/path/to/cert_bundle.pem'

response = requests.get('https://example.com', verify=cert_bundle_path)

print(response.text)

In the above code, we import the Requests Library and set the path of our certificate bundle file to 'cert_bundle_path'. We then make a GET request to 'https://example.com' and pass our certificate bundle path to the verify parameter.