I'm using a module that uses httpx
to make a request. Because of my company's network policy, I get a CERTIFICATE_VERIFY_FAILED
every time I try to run my code. The data I'm sending and receiving in the request is not sensitive so I want to disable the SSL verification on the module.
If I were to write the request I could use a session as such:
client = httpx.Client(**kwargs, verify=False)
But the request is inside the module.
Blender did an excellent response on this stack overflow answer where they wrote a context manager with the code bellow but the code does not work for the httpx
module:
import warnings
import contextlib
import requests
from urllib3.exceptions import InsecureRequestWarning
old_merge_environment_settings = requests.Session.merge_environment_settings
@contextlib.contextmanager
def no_ssl_verification():
opened_adapters = set()
def merge_environment_settings(self, url, proxies, stream, verify, cert):
# Verification happens only once per connection so we need to close
# all the opened adapters once we're done. Otherwise, the effects of
# verify=False persist beyond the end of this context manager.
opened_adapters.add(self.get_adapter(url))
settings = old_merge_environment_settings(self, url, proxies, stream, verify, cert)
settings['verify'] = False
return settings
requests.Session.merge_environment_settings = merge_environment_settings
try:
with warnings.catch_warnings():
warnings.simplefilter('ignore', InsecureRequestWarning)
yield
finally:
requests.Session.merge_environment_settings = old_merge_environment_settings
for adapter in opened_adapters:
try:
adapter.close()
except:
pass
Is there a way I could replicate the same behavior inside a request made through httpx?
source https://stackoverflow.com/questions/75726455/disable-ssl-verification-on-a-third-party-module-using-httpx
Comments
Post a Comment