Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify URL resolution in python's requests library in a similar fashion to curl's --resolve flag?

I am writing some python client code and, due to some environmental constraints, I want to specify a URL and also control how it is resolved. I can accomplish this with curl by using the --resolve flag. Is there a way to do something similar with Python's requests library?

Ideally this would work in Python 2.7 but I can make a 3.x solution work as well.

like image 675
Buck Avatar asked Jun 05 '17 17:06

Buck


1 Answers

Late answer, but there's a module called forcediphttpsadapter that does exactly this:

Install:

pip3 install forcediphttpsadapter

Usage:

import requests
from forcediphttpsadapter.adapters import ForcedIPHTTPSAdapter

url = 'https://domain.tld/path'
session = requests.Session()
session.mount(url, ForcedIPHTTPSAdapter(dest_ip='x.x.x.x')) # type the desired ip
r = session.get(url, verify=False)
print(r.text)
...

Sources:

  • Forcing Python Requests to connect to a specific IP address
  • Github repo: Roadmaster/forcediphttpsadapter
like image 151
Pedro Lobito Avatar answered Oct 06 '22 19:10

Pedro Lobito