Select outgoing IP from django? - python

If we're running on a host which can have multiple IP addresses (it's actually EC2 with elastic IPs), is it possible to select from django which outgoing IP address to use?
Even if this is just a random choice it'd be fine.
Edit: Apologies, I was not clear above. The requests are new outgoing calls made from within Python, not a response to a client request - happy for that to go back down whatever pipe it came in on.

I guess that for webapp responses, the server is always going to use one connection socket, so if the request came to IP address X, the response will be sent in the same TCP connection and will originate from the same address X, even though the host also has addresses Y and Z.
On the other hand, if your application creates another TCP connection during its operation, its probably possible to bind that socket on any of host's IP addresses you want. If you're using python's socket module, you can do it by specifying source_address argument in socket.create_connection() call. Unfortunately, not all higher-level libraries may allow this level of control.

I am not sure about the question quite well, but just wanted to drop this page, if it comes to any help python outgoing ip

Related

Changing IP of python requests

How do I change the IP of HTTP requests in python?
My friend built an API for a website, and sometimes it blocks certain IP's and so we need to change the IP of the request... here is an example:
login_req = self.sess.post('https://www.XXX/YYY', params={...}
Now, each request that it sends, is through the computer's IP, and we need it basically to pass through an imaginary VPN.
Thanks for the help. If something isn't clear I will explain.
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.
You can use an IP address from https://www.sslproxies.org/
For example,
import requests
response=requests.get("yourURL", proxies={'https': 'https://219.121.1.93:80', 'http': http://219.121.1.93:80 "})
The IP addresses on that site are pretty crappy and sometimes don't work, so it would be best to find a way to constantly scrape IP addresses from the site so you have a couple to try. Check out this article: https://www.scrapehero.com/how-to-rotate-proxies-and-ip-addresses-using-python-3/
warning: These should not be used for sensitive information as they are not secure. Don't use those IP addresses unless you are ok with anyone in the world knowing what your're doing.

Listen for a specific IP address? (Python 3.x)

I'm searching for a method to get a simple python server to listen for a specific local IP address on my LAN using the socket import (assuming the destination computer has a client script). socket.bind() and socket.listen(int) methods cannot provide me with any options for filtering IP addresses. Am I missing a method?
I don't think Python has a built-in method to do that.
However, as WhatsThePoint suggested, you can add your own filtering, as socket.recvfrom() will give you the client address, compare it with your required address, and if it's wrong, throw away the data.

How to listen to/forward all ports on an interface, in Python or otherwise

I am writing an application, currently in Python + Twisted, which serves as a first port of call for DNS requests – if the requests meet certain patterns, e.g. Namecoin .bit addresses or OpenNIC TLDs, they are passed to different DNS resolvers, otherwise the default one is used.
Some addresses however I need redirected through special routes, e.g. Tor .onion addresses which don't resolve to traditional IPv4 addresses, or certain websites that require tunneling through a VPN for geolocation reasons. So when DNS requests for such sites come in, I want the application to create a new loopback interface/alias and return the IP of this interface. I then need to be able to tunnel all TCP and UDP traffic coming through this interface through the proxy/VPN or whatever to the endpoint it was set up for.
The question is, how can I do this? Listening on specific ports (e.g. 80) will be fine for most purposes, but as a perfectionist I would like to know how to accept connections/messages sent to ALL ports, without having to set up tens of thousands of listeners and potentially crashing the system.
Note: while everything is currently in Python, I don't mind adding components in C++ or another language, or playing around with network configurations to get this working.

how to check if an ip address or proxy is working or not

How can I check if a specific ip address or proxy is alive or dead
Because there may be any level of filtering or translation between you and the remote host, the only way to determine whether you can connect to a specific host is to actually try to connect. If the connection succeeds, then you can, else you can't.
Pinging isn't sufficient because ICMP ECHO requests may be blocked yet TCP connections might go through fine.
Maybe this question here will help, its about pinging in python.
Post
An IP address corresponds to a device. You can't "connect" to a device in the general sense. You can connect to services on the device identified by ports. So, you find the ip address and port of the proxy server you're interested in and then try connecting to it using a simple socket.connect. If it connects fine, you can alteast be sure that something is running on that port of that ip address. Then you go ahead and use it and if things are not as you expect, you can make further decisions.

IP address of domain on shared host

I have domain on a shared hosting provider. How do I find the direct IP address of my domain using Python?
Is it possible to post to a script on my domain using the IP address and not the website itself?
Thanks.
I guess the IP should be static so do you really need to look it up more than once?
You need to specify the domain name so that the webserver knows which host configuration to use if you don't have a dedicated IP or your host is the default for that webserver
import socket
socket.gethostbyname("www.stackoverflow.com")
'69.59.196.211'
will get you the ip address (as a string) of your domain.
However, if it's shared hosting I would think it highly unlikely that you'll be able to access your hosting via the ip - most likely you'll have something like Apache's VirtualHost Directive in place which limits you to only 'seeing' requests to your domain. Requests to the IP address will be served by some default configuration.
Would very much depend on the nature of your hosting.
A curious request ...
To look up a domain name, do something like this:
import socket
ipaddress = socket.gethostbyname('www.bbc.co.uk')
Regarding posting to the IP address:
I don't think it would work in the normal way (like from a browser), because there will probably be many sites held under that address.
But, I guess you could do it in a very manual way, using a programming language (e.g. Python), if you connected a client socket to the site's IP address, but still sent the website's name in the HTTP Host request header.
I don't know if that poses more questions than it answers, and I don't know why you'd want to do either of the above, but there it is.
Good luck!

Categories