How do you disable SSL certificate verification in Python Requests? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you disable SSL certificate verification in Python Requests?

message-square
0

Pass verify=False to requests.get() / requests.post() (or set session.verify = False on a requests.Session) to disable TLS certificate verification for local development or self-signed endpoints—optionally silencing the resulting InsecureRequestWarning with urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning), while preferring verify="/path/to/ca-bundle.pem" in production so TLS server identity stays protected.

Basic Example

import requests
import urllib3

# 1. Disable SSL verification for a single request:
response = requests.get("https://localhost:8443/health", verify=False, timeout=10)
print(response.status_code)
# Output: 200 (also emits InsecureRequestWarning unless suppressed)

# 2. Suppress the InsecureRequestWarning & configure a reusable Session:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

session = requests.Session()
session.verify = False
res = session.post("https://localhost:8443/api/test", json={"ping": True}, timeout=10)
print(res.json())

# 3. Safer production alternative for internal/self-signed servers:
#    Trust your specific root CA .pem file instead of disabling verification completely!
safe_res = requests.get("[https://internal.corp.local/api](https://internal.corp.local/api)", verify="/path/to/internal-ca.pem", timeout=10)

Key Details & Edge Cases

  • verify=False triggers InsecureRequestWarning on every request unless suppressed:
    • urllib3 logs InsecureRequestWarning: Unverified HTTPS request is being made to host '...' to stderr every time verify=False runs.
    • Silence only that specific warning cleanly with urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) rather than calling warnings.filterwarnings("ignore") globally (which hides unrelated deprecation or resource warnings).
  • Environment variables REQUESTS_CA_BUNDLE and CURL_CA_BUNDLE override session.verify:
    • If REQUESTS_CA_BUNDLE or CURL_CA_BUNDLE is set in your OS environment, requests.Session.merge_environment_settings() can override session.verify = False unless you explicitly pass verify=False on the individual session.get(..., verify=False) call or set session.trust_env = False.
  • Never use verify=False on public networks or production APIs:
    • Disabling certificate verification still encrypts packets over TLS, but allows any active attacker on the network (public Wi-Fi, compromised router, malicious proxy) to perform a man-in-the-middle (MITM) attack with a forged certificate and read API keys, tokens, or payloads in plaintext.
    • For internal enterprise CAs or Zscaler/Netskope proxies, pass verify="/path/to/corporate-root-ca.pem" (or install pip-system-certs) instead of verify=False.
  • Self-signed client certificates (cert=) vs. server verification (verify=):
    • Do not confuse verify= (validating the server’s SSL certificate) with cert=("/path/to/client.crt", "/path/to/client.key") (mutual TLS client authentication). Even when using mutual TLS (cert=...), keep verify="/path/to/server-ca.pem" enabled.

Comparison of SSL Verification Options in Python requests

Option Syntax Validates Server Identity? Emits InsecureRequestWarning? Best Used When
verify=True (Default) requests.get(url) Yes (against certifi root CAs) No All production HTTPS requests to public APIs and websites.
verify="/path/to/ca.pem" requests.get(url, verify="ca.pem") Yes (against your custom CA file) No Corporate TLS proxies, staging servers, or self-signed internal services.
verify=False requests.get(url, verify=False) No (vulnerable to MITM attacks) Yes (unless silenced via urllib3) Temporary local testing against https://localhost dev servers with throwaway certs.
session.verify = False s = requests.Session(); s.verify = False No Yes Applying verify=False across multiple local test requests in one session (pair with s.trust_env = False if env vars interfere).