0

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?

João Areias
  • 1,192
  • 11
  • 41

1 Answers1

0

Works when module uses httpx.Client:

import contextlib


@contextlib.contextmanager
def no_ssl_verification():
    """Context manager to disable SSL verification on httpx Client.
    """
    import httpx

    # Save original Client constructor
    Client = httpx.Client

    # Disable SSL verification
    httpx.Client = lambda *args, **kwargs: Client(*args, verify=False, **kwargs)

    # Yield control to the caller
    yield

    # Restore original verify value
    httpx.Client = Client
João Areias
  • 1,192
  • 11
  • 41
  • Disable certificate (not "SSL") verification means you loose basically all benefits of using HTTPS, and you should then stop pretending to have any security at all and hence go back to plain HTTP because in practice this is what you have. – Patrick Mevzek Mar 14 '23 at 03:13
  • This is for a web scrapper, the data is not sensitive but the website I'm scrapping from always redirects to https, and the company's network monitoring intercepts the connection using a self-signed certificate to monitor the network, which breaks the code. I would use plain HTTP if I could. – João Areias Mar 14 '23 at 12:46