0

I am trying to override the IP address for the destination host on the fly using urllib3, while I am passing client certificates. Here is my code:

import urllib3
conn = urllib3.connection_from_url('https://MYHOST', ca_certs='ca_crt.pem', key_file='pr.pem', cert_file='crt.pem', cert_reqs='REQUIRED')
response = conn.request('GET', 'https://MYHOST/OBJ', headers={"HOST": "MYHOST"})
print(response.data)

I was thinking to use transport adapters, but I am not quite sure how to do it without using sessions.

Any thoughts or help?

Amir
  • 5,996
  • 13
  • 48
  • 61

1 Answers1

0

I guess we can follow the solutions presented here: Python 'requests' library - define specific DNS?

Hence a nasty way to do this is to override the hostname to the IP address we want by adding:

from urllib3.util import connection
import urllib3

hostname = "MYHOST"
host_ip = "10.10.10.10"
_orig_create_connection = connection.create_connection

def patched_create_connection(address, *args, **kwargs):
    overrides = {
        hostname: host_ip
    }
    host, port = address
    if host in overrides:
        return _orig_create_connection((overrides[host], port), *args, **kwargs)
    else:
        return _orig_create_connection((host, port), *args, **kwargs)

connection.create_connection = patched_create_connection  

conn = urllib3.connection_from_url('https://MYHOST', ca_certs='ca_crt.pem', key_file='pr.pem', cert_file='crt.pem', cert_reqs='REQUIRED')
response = conn.request('GET', 'https://MYHOST/OBJ', headers={"HOST": "MYHOST"})
print(response.data)

But, again based on the posted link, the better way to implement it is to have a proper adapter to override the IP address.

Amir
  • 5,996
  • 13
  • 48
  • 61