Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'url:pypi': 0.03; 'package,': 0.03; 'interfaces': 0.04; 'output': 0.05; 'explicit': 0.07; 'postgresql': 0.07; 'socket': 0.07; 'strict': 0.07; 'subject:same': 0.07; 'debugging.': 0.09; 'indication': 0.09; 'method:': 0.09; 'postgresql,': 0.09; 'tcp/ip': 0.09; 'subject:How': 0.10; 'python': 0.11; 'doing),': 0.16; 'filesystem': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'implies': 0.16; 'inclined': 0.16; 'normal,': 0.16; 'supplied': 0.16; 'tcp': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'app': 0.19; 'normally': 0.19; 'seems': 0.21; 'platforms': 0.22; '(in': 0.22; "aren't": 0.24; 'instance,': 0.24; 'parse': 0.24; 'simpler': 0.24; 'equivalent': 0.26; 'query': 0.26; '(for': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'host': 0.29; 'am,': 0.29; 'unix': 0.29; 'message- id:@mail.gmail.com': 0.30; '(which': 0.31; 'easier': 0.31; 'this.': 0.32; '(including': 0.33; 'url:python': 0.33; 'running': 0.33; 'core': 0.34; 'subject:the': 0.34; 'could': 0.34; 'connection': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'accessing': 0.36; 'method': 0.36; 'url:org': 0.36; 'application': 0.37; 'too': 0.37; 'two': 0.37; 'list.': 0.37; 'implement': 0.38; 'server': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'anything': 0.39; 'environment.': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'easy': 0.60; 'signal': 0.60; 'simply': 0.61; "you're": 0.61; 'address': 0.63; 'name': 0.63; 'within': 0.65; 'direct': 0.67; 'determine': 0.67; '26,': 0.68; 'started.': 0.68; "it'd": 0.84; 'one).': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=M8tWwe5vXGoXGaut/P1XHR0zJmEA9zcnPAV4mt4KZYY=; b=h4GppoRFHL8idze9t8ScT9Sl+n9t24oDhxKiJMgQ0yd3xxwkQJQMb5tIu4jLBXmbJ0 JNfDrdhukymlnODg/PDibBtnsRx9SBe0cNR8Q9KyjvAiFU+rFq2rW1uV7JoC8vZpxwrW A5vbLO8d/4Y7SovTSCuo9+FPoYzWQUUYNj+ml3yl6N7rPgGNljQPk3lm3BFG7wCDptEF PAlNFcJcw4xYE/eGOyfNH6oTHw8I+HDffKPB8NUXnL9y7s83AnTvE+0BPmujdd2k08kb IN9LS8f9NyeIvDNQLWzi2nC4xFqJZXQevKVn5KTeUhje0eXbLbC6D1Aq0cqA6W02almC Jrdg== MIME-Version: 1.0 X-Received: by 10.66.161.166 with SMTP id xt6mr4182021pab.169.1385420857672; Mon, 25 Nov 2013 15:07:37 -0800 (PST) In-Reply-To: References: Date: Tue, 26 Nov 2013 10:07:37 +1100 Subject: Re: How to determine whether client and server are on the same host From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385420866 news.xs4all.nl 15911 [2001:888:2000:d::a6]:53857 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60460 On Tue, Nov 26, 2013 at 6:35 AM, Malte Forkel wrote: > I have a Python application that communicates with a server via telnet. > Host and port of the server are supplied by the user when the > application is started. > > How can I determine from within the application whether the server's > host actually is the local host? (In that case I could implement an > operation not available in the telnet-based protocol by directly > accessing the local filesystem.) 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. 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: https://pypi.python.org/pypi/netifaces 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. 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. But it'd be even simpler - and in some ways better - to be explicit about "use a direct connection rather than TCP". ChrisA