Fixing SSLError in Python Requests: trusted bundles first, bypass only for testing
When Requests raises SSLError, it has refused to trust the certificate presented by an HTTPS server.
The durable remedy is to supply a bundle Requests can trust; turning verification off is a short-lived testing shortcut with a real security cost.
Why verification fails
Requests checks HTTPS certificates against the certifi CA bundle by default. If the presented certificate is self-signed, issued by a private CA, expired, or otherwise untrusted by the system, verification fails and no response is returned. The typical failure looks like this (module code0001):
- requests.exceptions.SSLError wrapping SSLCertVerificationError with CERTIFICATE_VERIFY_FAILED
- A self-signed certificate is the example cause in that captured error
The verify parameter controls this check. You therefore have two distinct paths: give Requests a certificate it can trust, or switch the check off. They are not equivalent in safety.
Fix 1: Use a trusted certificate
Use this path when you hold the site CA certificate or an internal CA bundle. Save it as a .pem file and pass its path to verify (module code0002). Replace the example URL and path with your own server and real certificate location. Success prints the server status code, such as 200, while the connection stays verified.
Decision checklist:
- Confirm the .pem file exists at the exact path before debugging the server; a missing file raises OSError: Could not find a suitable TLS CA certificate bundle, invalid path, which is a different failure from SSLError.
- verify also accepts a directory of certificates, but only after that directory has been processed with the OpenSSL c_rehash utility.
- To avoid repeating the argument, set session.verify on a requests.Session object, or export the REQUESTS_CA_BUNDLE environment variable.
Choose this fix for production, internal services, and any request carrying sensitive data.
Python example
import requests
custom_certificate_path = "./certificates/custom-certificate.pem"
response = requests.get(
"https://your-server.example/",
verify=custom_certificate_path
)
print(response.status_code)Fix 2: Disable verification only for local testing
For local development or temporary testing only, pass verify=False (module code0003). The request can then succeed and print 200 even against a self-signed certificate, but urllib3 emits an InsecureRequestWarning on every such call. Silencing it with urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) is possible, yet leaving the warning visible is better practice because it marks the connection as unverified.
Failure distinction to keep in mind: verify=False does not fix the certificate. It removes both certificate verification and the hostname check, so the connection loses protection against man-in-the-middle attacks. That trade-off is acceptable only briefly, locally, and without sensitive data.
Python example
import requests
response = requests.get("https://self-signed.badssl.com/", verify=False)
print(response.status_code)Quick decision guide
Start with the trusted-bundle fix and confirm the certificate path. Reach for verify=False only when you are testing locally and understand that you are trading away the security check. If the error changes to an OSError about a CA bundle path, fix the path rather than the server.
Frequently asked questions
How do I reuse a custom CA bundle across many requests?
Point verify at a .pem file or directory containing the correct CA material, or set session.verify / REQUESTS_CA_BUNDLE so every request uses that bundle. A missing path produces an OSError about a suitable TLS CA certificate bundle, not an SSLError, so check the path first.
Is disabling verification safe?
Passing verify=False skips certificate and hostname checks, leaving the connection exposed to man-in-the-middle attacks. Reserve it for local development or temporary testing, never production or sensitive data, and leave the InsecureRequestWarning visible as a reminder.