How to determine whether client and server are on the same host
malte.forkel at berlin.de
Tue Nov 26 12:23:49 CET 2013
Am 26.11.2013 00:07, schrieb Chris Angelico:
> Two easy ways you could do this. I would be inclined to do what
> PostgreSQL and others do, and have an explicit indication that you
> want to use a local method: for instance, the name "localhost". Use of
> anything else (including "127.0.0.1") means you use TCP/IP as normal,
> but the strict word "localhost", which is normally equivalent to the
> address 127.0.0.1, would signal your app to use the direct method (for
> PostgreSQL, that's a Unix socket rather than a TCP one). This means
> you can still force telnet to be used even if you're working locally -
> good for debugging.
Unfortunately, I can't rely on the user to use a specific way to specify
the (local) host.
> Alternatively, you can simply query the current network interfaces for
> their IPs, and see if the IP you were given is in that list. I don't
> know of a way to do that in core Python, but the C function you need
> is getifaddrs(), and this claims to wrap all that up nicely:
> This method would detect, for instance, that calling up 192.168.0.19
> port 12345 is a local lookup, because eth0 has address 192.168.0.19,
> and that 192.168.2.2 port 54321 is local too, because wlan0 has
> address 192.168.2.2.
That's a very nice module which I wasn't aware of. But if possible I
wouldn't want to force the user to install a (binary) extension module.
Finding the right module for his platform and version of Python might be
> If you aren't too concerned with running this on multiple platforms
> (which seems likely - working with the local filesystem implies you
> know a lot about what the server and client are doing), you could
> simply parse the output of 'ip addr', 'ipconfig', 'ifconfig', or some
> equivalent network utility for your platform. That might be easier
> than playing with the package, depending on environment.
The application is run under Windows as well. So I'd have to parse the
output of other network utilities as well, possibly in a variety of
Thanks for your suggestions! I'm sorry I'm so picky. May be there just
is no easy solution.
More information about the Python-list