0

I have several IP addresses configured on my network interface card. I need to specify through which IP address python requests must connect to the web server. Is it possible?

import requests
r = requests.get("http://example.com/foo/bar")

I need to force requests.get to perform http request through a dasignated IP address.

Is there any other http library that support this feature in python?

2 Answers2

1

You can use a SourceAddressAdapter from requests-toolbelt:

import requests
from requests_toolbelt.adapters import source

source = source.SourceAddressAdapter('127.0.0.1')

with requests.Session() as session:
    session.mount('http://', source)
    r = session.get("http://example.com/foo/bar")
Pelle
  • 1,222
  • 13
  • 18
0

Are you running a Django/Django-Rest project? Because, if so, you can just specify a custom host and port into the python manage.py runserver command! Like so:

python manage.py runserver 0.0.0.0:8001

Hope I understood the problem