Sling Academy
Home/Python/How to Fix SSL InsecurePlatform Error in Python Requests

How to Fix SSL InsecurePlatform Error in Python Requests

Last updated: January 02, 2024

Understanding the SSL InsecurePlatform Error

When you face the SSL InsecurePlatform error while using the Requests module in Python, it’s typically a sign that your environment doesn’t have the necessary packages or system capabilities to provide a secure connection over SSL. This error often occurs when using older versions of Python or when certain dependencies are not up to date.

Update Python and pip

Ensuring you have the latest versions of Python and pip can often resolve SSL errors.

  • Download the latest version of Python from the official website.
  • Upgrade pip to its latest version using the command pip install --upgrade pip.

Example:

import requests

# Make sure to update to Python 2.7.9 or greater
response = requests.get('https://example.com')
print(response.text)

Pros: Updating Python and pip can enhance security and performance, and provide support for newer TLS standards.

Cons: Updating might introduce incompatibilities with existing code, especially if you’re making a major version change.

Install Security Packages

To improve the Requests module’s ability to handle SSL connections, you can install a set of recommended security packages.

Run pip install requests[security] to install the extra packages:

  • pyOpenSSL
  • cryptography
  • idna

Code implementation:

import requests

# After installing requests[security], you can now establish a secure SSL connection
response = requests.get('https://example.com')
print(response.text)

Pros: Installing security packages often solves SSL issues without major changes to your environment.

Cons: Additional dependencies may need to be managed, and it could add overhead to your project.

Use PyOpenSSL Explicitly

If updating Python and installing security packages don’t work, you can use PyOpenSSL directly for a more controlled SSL environment.

  • Install PyOpenSSL using pip install pyopenssl.
  • Use urllib3’s PyOpenSSL support (which Requests uses internally).

A tiny code example:

import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()

import requests

# Now, your SSL connections use PyOpenSSL
response = requests.get('https://example.com')
print(response.text)

Pros: Direct use of PyOpenSSL can enable support for modern SSL and TLS features in environments where the standard library is limited.

Cons: It adds complexity to your project and requires understanding of both the Requests library internals and PyOpenSSL.

Troubleshoot Using Environment Variables

Sometimes, Python’s environment variables can affect SSL connections. By altering or updating these, you may resolve connection issues.

What to do here is just set environment variables like REQUESTS_CA_BUNDLE or CURL_CA_BUNDLE to the path of your cert file, as shown below:

# This is a shell command and needs to be run in your terminal
export REQUESTS_CA_BUNDLE=/path/to/certfile.pem

# In Python, after setting the environment variable
import requests

response = requests.get('https://example.com')
print(response.text)

Pros: Setting the environment variable is quick and might be necessary if your certificate bundle is in a non-standard location.

Cons: It’s a less generalized solution and you’ll need to manage these settings across different environments.

Switch to a Higher-Level Library

Using a higher-level HTTP client that wraps around Requests might abstract away the SSL issues by handling them internally:

  • Explore alternatives like http.client in Python 3 or third-party libraries like httpx.
  • Install with pip install httpx and change your client-side code accordingly.

Example:

import httpx

# Using httpx, which handles SSL issues internally 
response = httpx.get('https://example.com')
print(response.text)

Pros: A higher-level library may provide a more robust handling of SSL, especially if designed with updated practices.

Cons: This might require rewriting portions of your existing code to conform to the new library’s API.

Next Article: Python Requests: Download a File from an FTP Server

Previous Article: Fixing Python requests.exceptions.SSLError: dh key too small

Series: Python: Network & JSON tutorials

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types