Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!rt.uk.eu.org!news-transit.tcx.org.uk!newsfeed.xs4all.nl!newsfeed6.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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'distinct': 0.05; 'returned.': 0.07; '*name*': 0.09; '[0]': 0.09; 'header:In-reply- to:1': 0.09; 'operation,': 0.09; 'subject:error': 0.09; 'am,': 0.12; 'argument': 0.15; '(name,': 0.16; '+short': 0.16; 'instance:': 0.16; 'inverse': 0.16; 'name)': 0.16; 'nick': 0.16; 'rotation': 0.16; 'tuple,': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'jan': 0.19; 'cc:no real name:2**0': 0.20; 'string': 0.24; 'cc:2**0': 0.24; 'module': 0.26; "i'm": 0.26; 'function': 0.27; '(and': 0.28; 'order.': 0.29; 'cc:addr:python.org': 0.29; 'host': 0.30; '(unless': 0.30; 'confused': 0.30; 'chris': 0.30; 'equivalent': 0.31; 'usually': 0.31; 'quite': 0.32; 'list': 0.32; 'tue,': 0.32; 'normally': 0.34; 'dns': 0.34; 'doc': 0.34; 'uses': 0.36; 'element': 0.37; 'but': 0.37; 'list,': 0.37; "there's": 0.37; 'getting': 0.38; 'i.e.': 0.39; 'being': 0.39; 'help': 0.39; "it's": 0.40; 'address': 0.61; 'header:Received:6': 0.61; 'results': 0.63; 'number.': 0.66; '2012': 0.67; 'addresses,': 0.68; 'note:': 0.69; 'reply-to:no real name:2**0': 0.72; 'header:Reply-to:1': 0.84; '(first)': 0.84; 'addresses:': 0.84; 'subject:( ': 0.84; 'canonical': 0.91; 'subject:skip:s 20': 0.91 From: Nick Dokos To: Chris Angelico Subject: Re: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error In-reply-to: References: <24123dfe-b2fc-4f4c-8dfe-23bfef19b205@m10g2000vbc.googlegroups.com> <4F0B6BBE.5090501@codeaurora.org> Comments: In-reply-to Chris Angelico message dated "Tue, 10 Jan 2012 10:14:25 +1100." Organization: HPCS X-Mailer: MH-E 8.3.1; nmh 1.3; GNU Emacs 24.0.92 Date: Mon, 09 Jan 2012 18:32:44 -0500 Sender: nicholas.dokos@hp.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: nicholas.dokos@hp.com 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: 1326152352 news.xs4all.nl 6957 [2001:888:2000:d::a6]:56883 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18719 Chris Angelico wrote: > On Tue, Jan 10, 2012 at 9:35 AM, Dominic Binks wrote: > > The address database can have multiple names for the same IP address so > > gethostbyaddr returns a list - [0] being the first item in the list (and > > usually considered the canonical name) > > Point to note: The inverse operation, getting IP addresses from names, > quite frequently returns multiple results - and in that case, there's > no "canonical name". You'll still usually want to take the zeroth > (first) item in the list, but often the list will change in order. > Try, for instance: > > $ dig +short www.google.com > > which uses DNS rotation as a form of load-balancing. > > In any case, you normally just take the first entry. > On both 2.6.6 and 2.7.1, the doc says: ,---- | >>> help(socket.gethostbyaddr) | Help on built-in function gethostbyaddr in module _socket: | | gethostbyaddr(...) | gethostbyaddr(host) -> (name, aliaslist, addresslist) | | Return the true host name, a list of aliases, and a list of IP addresses, | for a host. The host argument is a string giving a host name or IP number. `---- so socket.gethostbyaddr(os.environ['REMOTE_ADDR'])[0] is the 0th element of the tuple, i.e. the *name* that is returned. So (unless I'm confused which is always a distinct possibility) this has nothing to do with load-balancing or multiple addresses: it's the equivalent of dig +short -x Nick