Requests: Sending HTTPS requests to specific destination IP address

Created on 12 Sep 2017  路  10Comments  路  Source: psf/requests

Using requests, is there a way to send HTTPS requests while specifying the destination IP address?

E.g. github.com has multiple IP address. If I do a DNS lookup for github.com from Hong Kong, I get 192.30.255.113 and 192.30.255.112.

What if I want to send a HTTPS request to a specific github IP? This is useful for when you setup a service with multiple IPs and you want to test whether all IPs work correctly. Or when a service has regional DNS records and you want to send requests to a particular regional server.

I wish I could do something like:

import requests
response = requests.get('https://github.com/', server_ip='192.30.255.113')

SSL certificate verification needs to still work, of course.

Most helpful comment

+1 for this feature

As this doesn't seem to be in, can this ticket be re-opened as feature request?

All 10 comments

Right now this is not supported, but you can achieve a similar effect using the hosts file on your machine. There is work ongoing to add the functionality needed to achieve this, however.

I know this issue is closed, but I'm wondering: is this not possible (or feasible) even if I implement a custom Transport Adapter (http://www.python-requests.org/en/master/user/advanced/#transport-adapters)?

Yes: if you do that you have access to an SSLContext and so can configure it appropriately.

I am glad that there is functionality being added. Unfortunately I cannot wait. So I am working on a TransportAdapter and found this link with DNSResolverHTTPAdapter
https://stackoverflow.com/questions/22609385/python-requests-library-define-specific-dns/22614367#22614367

Unfortunately it is replacing the FQDN with the ip address - I need to maintain the FQDN and ensure that the connection is made to the right IP address.

Also unable to find the SSLContext object that @Lukasa referenced. Hoping to get a bit more assistance to override the connection being made to the desired IP Address please.

@ram-ibm this is a defect tracker, not a support forum

@Lukasa

There is work ongoing to add the functionality needed to achieve this, however.

Hi. I'm also interested in something similar as the issue creator. Could you elaborate what you meant by the above comment? What sort of work was being done and is it completed?

+1 for this feature

As this doesn't seem to be in, can this ticket be re-opened as feature request?

Currently I have created a docker environment with each container setting a different /etc/hosts.

If this could be done using requests that would be a huge step forward

There is a good thread to address this need:
https://stackoverflow.com/questions/44374215/how-do-i-specify-url-resolution-in-pythons-requests-library-in-a-similar-fashio

In case someone else stumbles on this closed issue needing this feature.

Does this apply to when using http proxies? Will the DNS resolution happen client side (so that I can use the host file to override the DNS server) or will it happen in the proxy side? Thanks.

Was this page helpful?
0 / 5 - 0 ratings