How to get Python requests to trust a self signed SSL certificate?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Python requests verifies TLS certificates by default, so self signed certificates fail unless you explicitly trust the signing certificate. The correct fix is to provide a trusted CA bundle path, not disable verification globally. Secure trust configuration keeps development environments functional without weakening transport security.
Why Verification Fails
A self signed certificate is not signed by a public CA trusted by system bundles. requests uses certifi or system trust to validate server certificate chains, so unknown issuers trigger SSLError.
Correct Approach: Trust A Custom CA File
Pass the certificate or CA bundle path via verify.
This keeps verification enabled while extending trust with your certificate.
Session Wide Trust Configuration
For repeated calls, configure a session once.
This avoids repeating verify argument everywhere.
Environment Variable Option
requests supports REQUESTS_CA_BUNDLE environment variable.
Useful in CI and containerized environments where code should remain unchanged.
Temporary Bypass For Local Debug Only
You can disable verification with verify=False, but this is insecure and should be limited to short local debugging sessions.
Never use this in production or shared test environments with sensitive data.
Certificate Chain And Hostname Checks
Even with custom CA trust, requests can fail if:
- Certificate hostname does not match URL.
- Intermediate certificates are missing.
- Certificate is expired or malformed.
Validate with OpenSSL tools and ensure full chain is presented by server.
Container And CI Setup
In containers, mount CA files and configure environment variable at runtime. Avoid embedding private certificates directly in source repository unless policy allows it.
Example Docker runtime argument:
This keeps trust artifacts external and rotatable.
Security Best Practices
- Treat internal CA certs as configuration assets with change control.
- Rotate certificates before expiry.
- Use separate trust bundles per environment where needed.
- Add smoke tests that validate TLS handshake in CI.
This prevents last minute outages due to certificate drift.
Developer Experience Improvements
Provide a bootstrap script that installs dev certificates and configures REQUESTS_CA_BUNDLE automatically for local environments. This avoids repeated manual setup and reduces support noise for new contributors.
Pair this with clear troubleshooting docs that explain common SSL errors and where certificate files should live on each platform. Consistent onboarding prevents insecure shortcuts like permanent verify=False usage.
Regular certificate expiry monitoring should be part of operational alerts so trust bundles are rotated before incidents occur.
For shared teams, publish a single canonical CA bundle path convention so scripts and services do not diverge by machine.
Common Pitfalls
- Using
verify=Falseas permanent fix. - Trusting server leaf certificate instead of CA chain when chain rotation is expected.
- Forgetting hostname mismatch checks.
- Hardcoding local file paths that break in CI or containers.
- Committing private cert materials without governance.
Summary
- Self signed cert errors are expected until trust is configured.
- Use
verify=/path/to/ca.pemorREQUESTS_CA_BUNDLEfor secure trust setup. - Keep verification enabled in all non-debug environments.
- Validate certificate chain and hostname if errors persist.
- Manage trust files as environment configuration, not ad hoc code hacks.
Related reading
- How to get running pod status via Rest API
- How to get self pod with kubernetes client-go
- How to get status code from webclient?
- How to get the cpu usage per thread on windows win32
- How to get token from service account?
- How to handle HTTP OPTIONS requests in Spring Boot?
- How to get sample indices from RandomUnderSampler in imblearn
- How to get skbio PCoA Principal Coordinate Analysis results?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.