Disable SSL validation without using verify = False in Python requests

When developing using the corporate network, I get caught in various traps such as Proxy. The other day, when I tried to use a certain module in Python, the SSL server certificate verification failed in requests inside the module, and requests.exceptions.SSLError occurred.

Normally, to disable SSL server certificate validation with requests, write:

requests.get("https://www.google.com", verify=False)

However, this method requires code modification and cannot be used for requests inside the module. After investigating, I found a way to hack the following requests, so I will introduce it.

manner

Set the environment variable CURL_CA_BUNDLE to an empty string.

import os
os.environ['CURL_CA_BUNDLE'] = ''

How it works

requests has the process of overwriting verify with the environment variables CURL_CA_BUNDLE and REQUESTS_CA_BUNDLE as follows. Hack this to make verify`` False.

requests/session.py


            if verify is True or verify is None:
                verify = (os.environ.get('REQUESTS_CA_BUNDLE') or
                          os.environ.get('CURL_CA_BUNDLE'))

Instead of actually making it False, make it an empty string that evaluates to False. The reason why None, which is also evaluated as False, is useless and is an empty string is that None will be overwritten with True later.

To briefly explain how Python's ʻor works, ʻA or B returns ʻA when ʻA == True and B when ʻA == False`. Therefore

>>> None or None
None
>>> '' or None
None
>>> None or ''
''

It will be. You can use this to set the environment variable CURL_CA_BUNDLE to an empty string and make verify an empty string, that is, False.

Recommended Posts

Disable SSL validation without using verify = False in Python requests
Disable python2.6 ssl3 protocol in centos6
Log in to Slack using requests in Python
Save images using python3 requests
Run LINE Bot implemented in Python (Flask) "without using Heroku"
Try building a neural network in Python without using a library
[Python] JSON validation using Voluptuous
Translate using googletrans in Python
Using Python mode in Processing
GUI programming in Python using Appjar
Retry post request using python requests
Precautions when using pit in Python
[Python3] Google translate google translate without using api
Try using LevelDB in Python (plyvel)
Create Gmail in Python without API
Using global variables in python functions
Slice without using Python, colon (:). a.__getitem__ (slice (3,5)).
Detect keystrokes in Python (without Enter)
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Handwriting recognition using KNN in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
When using regular expressions in Python
GUI creation in python using tkinter 2
Create a record with attachments in KINTONE using the Python requests module
Mouse operation using Windows API in Python
Notes using cChardet and python3-chardet in Python 3.3.1.
Try using the Wunderlist API in Python
GUI creation in python using tkinter part 1
Get Suica balance in Python (using libpafe)
(Bad) practice of using this in Python
Slowly hash passwords using bcrypt in Python
Using venv in Windows + Docker environment [Python]
[FX] Hit oanda-API in Python using Docker
Tweet using the Twitter API in Python
[Python] [Windows] Serial communication in Python using DLL
I tried using Bayesian Optimization in Python
Get Youtube data in Python using Youtube Data API
Using physical constants in Python scipy.constants ~ constants e ~
Scraping a website using JavaScript in Python
Develop slack bot in python using chat.postMessage
Write python modules in fortran using f2py
Draw a tree in Python 3 using graphviz
Notes for using python (pydev) in eclipse
Disease classification in Random Forest using Python
Call APIGateWay with APIKey in python requests
Download files in any format using Python
Parallel task execution using concurrent.futures in Python
Notes on using code formatter in Python
Meaning of using DI framework in Python
Create an elliptical scatter plot in Python without using a multivariate normal distribution