When making HTTP requests in Python using the popular requests library, SSL certificate verification is enabled by default for security. However, sometimes you may want to disable SSL verification, like when testing locally or connecting to a server with a self-signed certificate.
Here are a few methods to make Python requests without SSL verification.
Disable SSL Verification for a Session
You can disable SSL verification for a requests session by setting
import requests
session = requests.Session()
session.verify = False
response = session.get("https://example.com")
This will disable verification for all requests made with this session.
Disable Per Request
To disable SSL verification for a single request, pass in
response = requests.get("https://example.com", verify=False)
Provide a Custom SSL Context
For more control, you can create a custom SSL context that doesn't verify certificates:
import ssl
import requests
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
response = requests.get("https://example.com", ssl_context=context)
Ignore SSL Warnings
You can also suppress the SSL warnings if you just want to ignore them:
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
response = requests.get("https://example.com")
The best option depends on your specific use case. Just remember that disabling SSL verification reduces security, so only do it when necessary.