CodexBloom - Programming Q&A Platform

Handling SSL Certificate Verification Errors with Requests in Python 3.9 while Connecting to a Local API

πŸ‘€ Views: 470 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-26
python-3.x requests ssl api Python

I recently switched to I'm working on a project and hit a roadblock... I've been struggling with this for a few days now and could really use some help. I'm working with an scenario with SSL certificate verification when trying to connect to a local API using the `requests` library in Python 3.9. The local API is running on a self-signed certificate, and I'm getting the following behavior: ``` requests.exceptions.SSLError: [SSL: UNSUPPORTED_PROTOCOL] unsupported protocol (_ssl.c:1007) ``` I've tried disabling verification by setting `verify=False` in my request, but this leads to another behavior: ``` UserWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. ``` Here’s the code I'm using: ```python import requests url = 'https://localhost:5000/api/data' response = requests.get(url, verify=False) print(response.json()) ``` I understand the warning, but I just want to test my API locally without dealing with certificates for now. I've also tried setting up a custom SSL context, but it doesn't seem to resolve the scenario. Here’s what I tried: ```python import ssl import requests context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE response = requests.get(url, verify=context) ``` This code snippet still raises the same `SSLError`. Any advice on how to properly handle SSL certificate verification for a local self-signed API without working with these errors? Is there a best practice I should follow in this scenario? Thanks! For context: I'm using Python on macOS. Thanks in advance! I'm working on a web app that needs to handle this. Could this be a known issue?