InsecureRequestWarning is a warning that occurs when a request is made without certificate verification. In Python, this warning happens for requests sent from requests and urllib libraries. By default, both libraries implement SSL verification to enable a secured connection.
Note: Sending requests without verification of certificates exposes you to security threats like man-in-the-middle attacks. It is best to avoid this method for scripts used at the production level or when sending and receiving personal data.
Here is a simple example of how we can reproduce the warning.
import requests response = requests.get("https://api.github.com/users", verify=False) print(response)
InsecureRequestWarning: Unverified HTTPS request is being made to host 'api.github.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings warnings.warn( <Response >
The request was sent successfully (shown by a status code of 200 on the response), but the requests library issues a warning that the HTTPS request is unverified. This is because we have verify=False.
We will discuss the following items regarding how to disable InsecureRequestWarning.
- Suppress InsecureRequestWarning in requests,
- Disable InsecureRequestWarning in the urllib package,
- Eliminate InsecureRequestWarning using the warnings package, and,
- Remove InsecureRequestWarning by explicitly issuing CA bundles for verification.
In this case, we need to add one line, as shown in the code snippet below. Once the warnings are disabled, we can send requests without SSL verification without requests giving InsecureRequestWarning.
import requests # the following line is responsible for suppressing the warning. requests.packages.urllib3.disable_warnings() response = requests.get("https://api.github.com/users", verify=False) print(response)
Note: This method suppresses all the warnings from requests.
In this case, we need to import the urllib3 package and use it to disable warnings, as shown below.
import urllib.request import ssl # the following two lines disable InsecureRequestWarning import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # Call urlopen without SSL verification response = urllib.request.urlopen("https://www.example.com", context=ssl._create_unverified_context()) print(response)
<http.client.HTTPResponse object at 0x7f127492f7f0>
Let’s revisit the InsecureRequestWarning message shown at the beginning of the article.
InsecureRequestWarning: Unverified HTTPS request is being made to host ‘api.github.com’. Adding certificate verification is strongly advised…
The warnings package will take the warning message (it comes after the warning name) and filter the warnings accordingly. Here is an example,
import requests # Import warnings and use it to filter the warnings based on the warning message import warnings warnings.filterwarnings('ignore', message='Unverified HTTPS request') response = requests.get("https://api.github.com/users", verify=False) print(response)
The advantage of this method is that it filters specific warnings, not all of them in one go.
This is a manual way of SSL verification. Download the bundle from https://mkcert.org/generate/. If you can’t get the file from that link, follow these steps:
- Go to https://certifiio.readthedocs.io/en/latest/,
- Scroll down to the section “How do I use it?” and,
- Download the raw CA Bundle
import requests # make sure to issue path to the CA bundle file you downloaded on verify, # that is, verify="/path/to/the/CA bundle." In my case, the bundle is named # certs.pem, located in the current directory. response = requests.get("https://api.github.com/users", verify="certs.pem") print(response)