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?

4
  • It is possible but if you need help for the implementation you must be more specific about the devices and tools you use for it. Commented Jun 28, 2022 at 10:29
  • i think it's clear. if you have specific question, just ask Commented Jun 28, 2022 at 10:34
  • No, i have One interface with multi IP addresses. I don't have multi network interfaces Commented Jun 28, 2022 at 10:52
  • Probably this will help you stackoverflow.com/questions/12585317/requests-bind-to-an-ip Commented Jun 28, 2022 at 11:02

2 Answers 2

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")
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Post updated. i don't use Django

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.