Possible to Assign a New Ip Address on Every Http Request

Possible to assign a new IP address on every http request?

If you have a large pool of proxies you can use, then I suppose you could switch between them, but otherwise no, you generally can't just renew your IP address whenever you feel like it.

You might want to look into Tor, an anonymizing network which does something like what you're asking about.

Python requests, change IP address

As already mentioned in the comments and from yourself, changing the IP could help. To do this quite easily have a look at vpngate.py:

https://gist.github.com/Lazza/bbc15561b65c16db8ca8

An How to is provided at the link.

Can a single machine make requests from multiple different IP Addresses?

I think that the easiest way is to create a list of proxy servers and send every K (10 in your example) requests via a different proxy server. So if your list include N proxy servers you can send N*K requests per day.

You can setup your own servers or use different proxy server providers, Some of them are free.

See: how to use proxy with python

Changing IP of python requests

Short answer: you can't.

Long answer: it seems like you're misunderstanding how IP addresses work. Your IP address is the network address that corresponds to your computer - when you send a request to a server, you attach your IP as a "return address" of sorts, so that the server can send a response back to you.

However, just like a physical address, you don't get to choose what your IP address is – you live on a street, and that's your address, you don't get to change what the street is called or what your house number is. In general, when you send a request from your computer, the message passes through a chain of devices. For example:

Your computer --> Your router --> Your ISP --> The Server

In a lot of cases, each of these assigns a different IP address to whatever's below it. So, when your request passes through your router, your router records your IP address and then forwards the request through your ISP using its own IP address. Hence how several users on the same network can have the same IP address.

There are physical IP addresses, that correspond directly to devices, but there are a limited amount of these. Mostly, each Internet Service Provider has a few blocks of IP addresses that it can attach to things; an ISP can keep a specific IP address pointed to a specific computer all of the time, but they don't have to, and for many of their regular users, they don't.

Your computer has basically no power to determine what its own IP address is, basically. There's nothing python can do about that.

Your Question:

we need [the request] basically to pass through an imaginary VPN.

It'd be easier to actually requisition a real proxy or VPN from somewhere and push your request through it. You'd have to talk with your internet service provider to get them to set something like that up for you specifically, and unless you're representing a reasonably big company they're unlikely to want to put in that effort. Most python libraries that deal with HTTP can easily handle proxy servers, so once you figure it out it shouldn't be a problem.

how to change originating IP in HttpWebRequest

According to this, no. You may have to drop down to using Sockets, where I know you can choose the local IP.

EDIT: actually, it seems that it may be possible. HttpWebRequest has a ServicePoint Property, which in turn has BindIPEndPointDelegate, which may be what you're looking for.

Give me a minute, I'm going to whip up an example...

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com");

req.ServicePoint.BindIPEndPointDelegate = delegate(
ServicePoint servicePoint,
IPEndPoint remoteEndPoint,
int retryCount) {

if (remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) {
return new IPEndPoint(IPAddress.IPv6Any, 0);
} else {
return new IPEndPoint(IPAddress.Any, 0);
}

};

Console.WriteLine(req.GetResponse().ResponseUri);

Basically, the delegate has to return an IPEndPoint. You can pick whatever you want, but if it can't bind to it, it'll call the delegate again, up to int.MAX_VALUE times. That's why I included code to handle IPv6, since IPAddress.Any is IPv4.

If you don't care about IPv6, you can get rid of that. Also, I leave the actual choosing of the IPAddress as an exercise to the reader :)

How to deal with dynamic IP addresses of clients on a HTTP server?

Tracking users by IP address is considered bad practice. It causes a vast range of problems - for example, like in your case, IP addresses can change very easily and are inconsistent. Also, sometimes you have many users originating from the same IP address. Until recently, some countries even had one IP address allocated for the whole country.

A more common practice is to use HTTP sessions. They let you track users very accurately and a lot of infrastructure and libraries already exist for their management.

Assign IPs to programs/processes

As per @LeonardoRick request, I'm providing the details for the solution that I ended up with.

Say, I have a server with 172.16.0.1 and 172.16.0.2 IP addresses.

I set up nginx (on the same machine) with the configuration that was looking somewhat like this:

server {

# NEVER EXPOSE THIS SERVER TO THE INTERNET, MAKE SURE PORT 10024 is not available from outside
listen 127.0.0.1:10024;

# block access from outside on nginx level as well
allow 127.0.0.1;
deny all;

# actual proxy rules
location ~* ^/from-172-16-0-1/http(s?)\:\/\/(.*) {
proxy_bind 172.16.0.1;
proxy_pass http$1://$2?$args;
}
location ~* ^/from-172-16-0-2/http(s?)\:\/\/(.*) {
proxy_bind 172.16.0.2;
proxy_pass http$1://$2?$args;
}
}

(Actually I cannot remember all the details now (this code is 'from whiteboard', it's not an actual working one), nevertheless it should represent all the key ideas. Check regexes before deployment).
Double-check that port 10024 is firewalled and not accessible from outside, add extra authentication if necessary. Especially if you are running Docker.

This nginx setup makes it possible to run HTTP requests like
http://127.0.0.1:10024/from-172-16-0-2/https://example.com/some-URN/object?argument1=something

Once received a request, nginx will fetch the HTTP response from the requested URL using the IP specified by the corresponding proxy_bind directive.

Then - as I was running in-house or open-source software - I simply configured it (or altered its code) so it would perform requests like the one above instead of (original) https://example.com/some-URN/object?argument1=something.

All the management - what IP should be used at the moment - was also done by 'my' software, it simply selected the necessary /from-172-16-0-XXX/ endpoint according to its business logic.

That worked very well for my original question/task. However, this may not be suitable for some other applications, where it could not be possible to alter the request URLs. However, a similar approach with setting some kind of proxy may work for those cases.


(If you are not familiar with nginx, there are some starting guides here and here)

Is it possible to obtain the IP address from a HTTP Request

Sure. Use cgi, and os. The client's IP address is located in the environ variable with a title of REMOTE_ADDR. For example, the following will print out the client's ip address:

import cgi
import os

print "Content-type: text/html"
print ""
print cgi.escape(os.environ["REMOTE_ADDR"])


Related Topics



Leave a reply



Submit