Skip to content

Instantly share code, notes, and snippets.

@rcbadiale
Created January 5, 2022 12:18
Show Gist options
  • Select an option

  • Save rcbadiale/6d785b60314d6531eaf417e71e888364 to your computer and use it in GitHub Desktop.

Select an option

Save rcbadiale/6d785b60314d6531eaf417e71e888364 to your computer and use it in GitHub Desktop.
A function to patch the urllib3 / requests to print and/or change the hosts as needed.
from urllib3.util import connection
def patch_requests_connector():
_create_connection = connection.create_connection
def patched_create_connection(address, *args, **kwargs):
"""
Patches the create_connection in order to print the hosts.
This is useful in cases of the requests being redirected to
unavailable hostnames, with this you can check which host:port
is being used in every request made.
If needed this patch can be modified to resolve the names into
different routes.
"""
host, port = address
print(address, host, port)
return _create_connection((host, port), *args, **kwargs)
connection.create_connection = patched_create_connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment