Using requests.utils, you can check whether a connection to a given URL will go through a proxy.

When configuring an environment, it is always possible that some settings may go awry, perhaps they have been assigned to a particular user, rather than globally; maybe they are completely misconfigured. If there is no validation or verification applied to a given value, then small errors could lead to big problems.

One such situation is when configuring an environment that accesses the internet through a proxy, but bypasses that proxy internally. If you fail to set up a proxy, then it cannot access external APIs. If you fail to configure NO_PROXY correctly, then it can place undue load on your proxy, or even completely fail to work.

Requests offers ways to test some of your proxy configurations without making requests.

You can examine the settings using requests.utils.getproxies, thus:

 >>> requests.utils.getproxies()
    'http': '', 
    'https': '', 
    'no': 'localhost,,'

You can also check whether a given URL will go through the proxy, with requests.utils.should_bypass_proxies, thus:

>>> requests.utils.should_bypass_proxies('', None)

>>> requests.utils.should_bypass_proxies('', None)

One thing to note is that should_bypass_proxies checks no_proxy independently of http_proxy and https_proxy. It will return False as long as the URL is not matched by no_proxy, regardless of whether there is a proxy for it to bypass.

To perform the check in one step, you can use select_proxy, however, you will also need to pass in the proxy information as a dict, thus.

>>> proxies = requests.utils.get_environ_proxies('')
>>> requests.utils.select_proxy('', proxies)
>>> del proxies['http']
>>> requests.utils.get_environ_proxies()

This is ideal for a post-install check in such an application, perhaps to be exercised by an ansible smoke test