My Coding > Programming language > Python > Python and WWW > Requests > How to use different IP for HTTP requests

How to use different IP for HTTP requests

Some servers have more than one IP. In this case (and only in this case) you can choose which IP to use for performing HTTP requests. This is very convenient, when you need to model behaviours of a few independent computers from the same server.

This is different from using proxy!!!

To change your IP, it is necessary to use requests_toolbelt.adapters library and bind your session with new IP address.

So, let’s imagine for example, that the default IP address for your server is 'yyy.yyy.yyy.yyy', but your server also linked with IP address ''

To check your IP, I will use server

import requests
from requests_toolbelt.adapters import source

IP = '' # this is extra server IP
# The default server IP is 'yyy.yyy.yyy.yyy'

source = source.SourceAddressAdapter(IP)

with requests.Session() as session:
    session.mount('http://', source)
    r = session.get("")

print(r.text) #

x = requests.get("")
print(x.text)  # yyy.yyy.yyy.yyy

Published: 2023-02-04 15:41:06

Last 10 artitles

9 popular artitles

© 2020 -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.