Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
If you have a service that uses a specific port, and you have multiple computers on the same ip addess, how is this handled? Should the service specify to which computer on the ip address the information should be send? What if both computers on the same ip use the same service, but request different information?
Also, if a client is on a dynamic ip, how should the service detect that the ip has been changed, but the client (and the session) is the same? Should clients identify themselves for every request (much like cookies over http)?
You have many questions, I'll try to respond to them one by one.
If you have a service that uses a specific port, and you have multiple computers on the same ip addess, how is this handled?
Someone mentioned that multiple computers cannot have the same IP address. In the original IP model, this is true, though today such address sharing (through NAT) is common. But even in the original model, your question makes sense if you reformulate it slightly:
"If you have a service that uses a specific port, and you have multiple clients on the same ip address, how is this handled?"
There can be multiple client processes on the same host (thus sharing the same IP address) trying to contact the same server (using the same destination address+port combination). This was natural at the time IP was developed, as most machines powerful enough to connect to the network were multi-user machines. That's why TCP (and UDP) have port numbers on both sides (source and destination, or client and server). Client processes typically don't specify the source port when contacting a server, but an "ephemeral" source port is allocated to the socket by the host operating system for the lifetime of the socket (connection). So this is how the server distinguishes between clients from the same address: by their source ports.
NAT maps different hosts (with different "internal" IP addresses) to the same "external" IP addresses, but it also allocates unique source ports to outgoing packets. So the server sees this just like the original case (multiple client processes from the same "host"/IP address). The NAT then "demultiplexes" the server's responses to the different internal hosts.
Should the service specify to which computer on the ip address the information should be send? What if both computers on the same ip use the same service, but request different information?
The server does this by sending responses to the same address+port combination that the different clients used as source address/port. This is mostly handled automatically by the socket API. As described above, the two clients will get separate connections, and the server hopefully handles these as separate "sessions" and doesn't confuse requests between these sessions.
Also, if a client is on a dynamic ip, how should the service detect that the ip has been changed, but the client (and the session) is the same? Should clients identify themselves for every request (much like cookies over http)?
Now, this is a whole can of worms. If a service wants to "survive" client IP address changes, then it will have to use some other identifier. HTTP (session) cookies are a good example. TCP connections are broken by address changes - this is normal, as such changes weren't envisioned as part of normal operation when TCP/IP was designed. There have been attempts at making TCP/IP more robust against such changes, such as Mobile IP, MPTCP, and possibly SCTP, but none of these have really entered the mainstream yet. Basing your protocol on HTTP(S) and using session cookies may be your best bet.
I don't think I fully understand what you've said. There is no way that multiple computers will be on the same IP, this is not how the internet works.. There are protocols which hadels such things.
Did you mean that you're a server and multiple computers try connect to you?
If so, you listen in a port and when you get a connection you open a new thread for the service of that computer and the main loop still listening
Related
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.
I am hosting a http server on Python using BaseHTTPServer module.
I want to understand why it's required to specify the IP on which you are hosting the http server, like 127.0.0.1/192.168.0.1 or whatever. [might be a general http server concept, and not specific to Python]
Why can't it be like anybody who knows the IP of the machine could connect to the http server?
I face problems in case when my http server is connected to two networks at the same time, and I want to serve the http server on both the networks. And often my IP changes on-the-fly when I switch from hotspot mode on the http server machine, to connecting to another wifi router.
You must specify the IP address of the server, mainly because the underlying system calls for listening on a socket requires it. At a lower level you declare what pair (IP address, port) you want to use, listen on it and accept incoming connexions.
Another reason is that professional grade server often have multiple network interfaces and multiple IP addresses, and some services only need to listen on some interface addresses.
Hopefully, there are special addresses:
localhost or 127.0.0.1 is the loopback address, only accessible from local machine. It is currently used for tests of local services
0.0.0.0 (any) is a special address used to declare that you want to listen to all the local interfaces. I think that it is what you want here.
Try running it on 0.0.0.0, this accepts connections from all interfaces. Explicitly specifying the IP is a good practice in general (load balancing, caching servers, security, internal netwrok-only micro services, etc), but judging by your story this is not a production server, but some internal LAN application.
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.
I want to build a peer to peer chat engine that runs over the Internet. So far my code works on a local network but not further. This is due to the fact that listening on sockets using python sockets does not make them available outside of the LAN.
It is acceptable for IPs to be shared knowledge, ie it is ok for the other person to need to know my IP address (and a port on which I am listening) to connect to me.
How does one tell the router to open a socket to the outside world? Presumably this can be done as p2p software such as BitTorrent must do it for communication between clients.
As you have mentioned you have to open a specific port on the router and use that port for communication. As there are many router manufacturers each with a variety of models I suggest you to check the manual for the router you want to use.
for the code, you may check if your code works on LAN and then see if the router let's you white-list some ports. you may find many simple examples online.
this is a code i played sometime ago:
http://www.mediafire.com/download/vef4q4prkr7be2e/python.socket.zip
if you don't want users to mess up with ports and router settings and such, first alternative i can think of is this:
you setup an REST API, in one interface one is able to retrieve the messages providing (chatRoomName, FromTimestamp, ToTimestamp[,optionally chatRoomPassWord]) but this has nothing to do with sockets, you have to use simple HTTP requests(urllib/urllib2). Of course there might exist some workaround for this such as an always-white-listed port(like 80 for browsers, 22 for SSH) but you have to search for such exceptions.
note that ports up to 1024 require special privileges(admin/sudo) to be used.
p.s. in traditional implementation other party(client) have to know your (ip, port) duo to be able to connect to the you(server).
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