Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11997 > unrolled thread

Order of addresses returned by socket.gethostbyname_ex()

Started byTomas Lidén <tomas@liden-privat.net>
First post2011-08-21 23:37 -0700
Last post2011-08-22 20:12 +0100
Articles 17 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  Order of addresses returned by socket.gethostbyname_ex() Tomas Lidén <tomas@liden-privat.net> - 2011-08-21 23:37 -0700
    Re: Order of addresses returned by socket.gethostbyname_ex() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-22 18:15 +1000
      Re: Order of addresses returned by socket.gethostbyname_ex() Tomas Lidén <tomas.liden.privat@gmail.com> - 2011-08-22 02:06 -0700
        Re: Order of addresses returned by socket.gethostbyname_ex() Cameron Simpson <cs@zip.com.au> - 2011-08-22 20:06 +1000
          Re: Order of addresses returned by socket.gethostbyname_ex() Tomas Lidén <tomas.liden.privat@gmail.com> - 2011-08-22 04:29 -0700
            Re: Order of addresses returned by socket.gethostbyname_ex() Cameron Simpson <cs@zip.com.au> - 2011-08-23 08:46 +1000
        Re: Order of addresses returned by socket.gethostbyname_ex() Chris Angelico <rosuav@gmail.com> - 2011-08-22 11:36 +0100
          Re: Order of addresses returned by socket.gethostbyname_ex() Tomas Lidén <tomas.liden.privat@gmail.com> - 2011-08-22 04:32 -0700
        Re: Order of addresses returned by socket.gethostbyname_ex() Roy Smith <roy@panix.com> - 2011-08-22 07:37 -0400
          Re: Order of addresses returned by socket.gethostbyname_ex() Paul Kölle <paul@subsignal.org> - 2011-08-22 14:18 +0200
            Re: Order of addresses returned by socket.gethostbyname_ex() Tomas Lidén <tomas.liden.privat@gmail.com> - 2011-08-22 05:33 -0700
        Re: Order of addresses returned by socket.gethostbyname_ex() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-23 02:09 +1000
    Re: Order of addresses returned by socket.gethostbyname_ex() Roy Smith <roy@panix.com> - 2011-08-22 07:26 -0400
      Re: Order of addresses returned by socket.gethostbyname_ex() Tomas Lidén <tomas.liden.privat@gmail.com> - 2011-08-22 04:39 -0700
        Re: Order of addresses returned by socket.gethostbyname_ex() Roy Smith <roy@panix.com> - 2011-08-22 08:14 -0400
        Re: Order of addresses returned by socket.gethostbyname_ex() Terry Reedy <tjreedy@udel.edu> - 2011-08-22 13:34 -0400
    Re: Order of addresses returned by socket.gethostbyname_ex() Nobody <nobody@nowhere.com> - 2011-08-22 20:12 +0100

#11997 — Order of addresses returned by socket.gethostbyname_ex()

FromTomas Lidén <tomas@liden-privat.net>
Date2011-08-21 23:37 -0700
SubjectOrder of addresses returned by socket.gethostbyname_ex()
Message-ID<356978ef-e9c1-48fd-bb87-849fe8e27a79@p5g2000vbl.googlegroups.com>
In what order are the addresses returned by socket.gethostbyname_ex()?

We know that gethostbyname() is indeterministic but hope that
gethostbyname_ex() has a specified order.

Best regards,
Tomas

[toc] | [next] | [standalone]


#11999

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-08-22 18:15 +1000
Message-ID<4e521039$0$29983$c3e8da3$5496439d@news.astraweb.com>
In reply to#11997
On Mon, 22 Aug 2011 04:37 pm Tomas Lidén wrote:

> In what order are the addresses returned by socket.gethostbyname_ex()?
> 
> We know that gethostbyname() is indeterministic but hope that
> gethostbyname_ex() has a specified order.

Did you want a particular order, or just any deterministic order?

Have you considered sorting the addresses yourself?



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#12000

FromTomas Lidén <tomas.liden.privat@gmail.com>
Date2011-08-22 02:06 -0700
Message-ID<c4163eeb-c306-49d1-9337-0eb9f47df52f@s12g2000yqm.googlegroups.com>
In reply to#11999
On 22 Aug, 10:15, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
> On Mon, 22 Aug 2011 04:37 pm Tomas Lidén wrote:
>
> > In what order are the addresses returned by socket.gethostbyname_ex()?
>
> > We know that gethostbyname() is indeterministic but hope that
> > gethostbyname_ex() has a specified order.
>
> Did you want a particular order, or just any deterministic order?
>
> Have you considered sorting the addresses yourself?
>
> --
> Steven

In this particular case we have a host with several connections (LAN,
WIFI, VmWare adapters etc). When using gethostbyname() we got a VmWare
adapter but we wanted to get the LAN (or the "best" connection to our
server). With gethostbyname_ex() the ordering seemed to become LAN,
Wifi, etc and that's good for us. But we don't know if this holds on
other platforms (we're running on Windows 7).

A cross-platform deterministic order would be excellent for us.

/Tomas

[toc] | [prev] | [next] | [standalone]


#12006

FromCameron Simpson <cs@zip.com.au>
Date2011-08-22 20:06 +1000
Message-ID<mailman.306.1314007617.27778.python-list@python.org>
In reply to#12000
On 22Aug2011 02:06, Tomas Lid�n <tomas.liden.privat@gmail.com> wrote:
| On 22 Aug, 10:15, Steven D'Aprano <steve
| +comp.lang.pyt...@pearwood.info> wrote:
| > On Mon, 22 Aug 2011 04:37 pm Tomas Lidén wrote:
| > > In what order are the addresses returned by socket.gethostbyname_ex()?
| >
| > > We know that gethostbyname() is indeterministic but hope that
| > > gethostbyname_ex() has a specified order.
| >
| > Did you want a particular order, or just any deterministic order?
| >
| > Have you considered sorting the addresses yourself?
| 
| In this particular case we have a host with several connections (LAN,
| WIFI, VmWare adapters etc). When using gethostbyname() we got a VmWare
| adapter but we wanted to get the LAN (or the "best" connection to our
| server). With gethostbyname_ex() the ordering seemed to become LAN,
| Wifi, etc and that's good for us. But we don't know if this holds on
| other platforms (we're running on Windows 7).
| 
| A cross-platform deterministic order would be excellent for us.

It would not surprise me if the order was related to the order a scan of
the system interfaces yields information, and I would imagine that may
be influenced by the order in which the interfaces were initialised.

So getting the LAN first may merely be fortuitous.
I wouldn't rely on it, especially if interfaces come and go.

What if you queried your routing table instead? Usually there's just one
default route, and hopefully it would be configured to use the "best"
interface.

Cheers,
-- 
Cameron Simpson <cs@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

If you give me six lines written by the most honest man, I will find
something in them to hang him.  - Cardinal Richilieu

[toc] | [prev] | [next] | [standalone]


#12012

FromTomas Lidén <tomas.liden.privat@gmail.com>
Date2011-08-22 04:29 -0700
Message-ID<bed10d56-f5dc-4ffc-b42b-1eeec5d6c619@w11g2000vbp.googlegroups.com>
In reply to#12006
On 22 Aug, 12:06, Cameron Simpson <c...@zip.com.au> wrote:
>
> It would not surprise me if the order was related to the order a scan of
> the system interfaces yields information, and I would imagine that may
> be influenced by the order in which the interfaces were initialised.
>
> So getting the LAN first may merely be fortuitous.
> I wouldn't rely on it, especially if interfaces come and go.
>

We did try to disable/enable the interfaces in different orders, but
always got the same return order from gethostbyname_ex(). So it looked
pretty stable (on that specific OS). There's been some testing on
Linux as well, but since this should be used in a general cross
platform tool we wanted to check the exact behaviour of the method.

> What if you queried your routing table instead? Usually there's just one
> default route, and hopefully it would be configured to use the "best"
> interface.
>

Hmm... perhaps. How would you do that?
/Tomas

[toc] | [prev] | [next] | [standalone]


#12054

FromCameron Simpson <cs@zip.com.au>
Date2011-08-23 08:46 +1000
Message-ID<mailman.331.1314053201.27778.python-list@python.org>
In reply to#12012
On 22Aug2011 04:29, Tomas Lid�n <tomas.liden.privat@gmail.com> wrote:
| On 22 Aug, 12:06, Cameron Simpson <c...@zip.com.au> wrote:
| > It would not surprise me if the order was related to the order a scan of
| > the system interfaces yields information, and I would imagine that may
| > be influenced by the order in which the interfaces were initialised.
| >
| > So getting the LAN first may merely be fortuitous.
| > I wouldn't rely on it, especially if interfaces come and go.
| 
| We did try to disable/enable the interfaces in different orders, but
| always got the same return order from gethostbyname_ex(). So it looked
| pretty stable (on that specific OS). There's been some testing on
| Linux as well, but since this should be used in a general cross
| platform tool we wanted to check the exact behaviour of the method.
| 
| > What if you queried your routing table instead? Usually there's just one
| > default route, and hopefully it would be configured to use the "best"
| > interface.
| 
| Hmm... perhaps. How would you do that?

On UNIX systems (and, I think, also Windows systems) you'd run the command:

  netstat -rn

or use the "route" command. (This avoids using operating system calls
directly - the command will know what to do itself.)

I seem to recall that Windows has the netstat command as well (possibly
because, IIRC, they pulled in the BSD UNIX IP stack for their first IP
implementation).

Anyway, by popening the netstat command you can pull the "default" (or
"0.0.0.0") route from its output. Then see if the interface listed there
corresponds to what you need to know.

Cheers,
-- 
Cameron Simpson <cs@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Then again, it's probably really because your average Australian doesn't give
a shit about anything that doesn't increase the price of beer.
        - Murray Chapman <muzzle@cs.uq.oz.au>

[toc] | [prev] | [next] | [standalone]


#12007

FromChris Angelico <rosuav@gmail.com>
Date2011-08-22 11:36 +0100
Message-ID<mailman.307.1314009375.27778.python-list@python.org>
In reply to#12000
On Mon, Aug 22, 2011 at 11:06 AM, Cameron Simpson <cs@zip.com.au> wrote:
> What if you queried your routing table instead? Usually there's just one
> default route, and hopefully it would be configured to use the "best"
> interface.
>

I wouldn't necessarily trust even this, on Windows. I've lately had
the most insane trouble getting my XP laptop to function properly as a
proxy - LAN connection has the default gateway, wifi has only
192.168.* - and Windows kept on trying to send DNS queries out on the
wireless connection, and then wondered why it didn't get a result.

Explicit is better than implicit. Instead of using the order, have a
config file that chooses the one(s) you want by name or IP address. Of
course, if you're on Unix/Linux, you can use the interface name (eth0,
eth1, etc) with a fair degree of reliability.

ChrisA

[toc] | [prev] | [next] | [standalone]


#12013

FromTomas Lidén <tomas.liden.privat@gmail.com>
Date2011-08-22 04:32 -0700
Message-ID<c8ae765e-a448-4779-8648-078fce3eaa08@l2g2000vbn.googlegroups.com>
In reply to#12007
On 22 Aug, 12:36, Chris Angelico <ros...@gmail.com> wrote:
>
> Explicit is better than implicit. Instead of using the order, have a
> config file that chooses the one(s) you want by name or IP address. Of
> course, if you're on Unix/Linux, you can use the interface name (eth0,
> eth1, etc) with a fair degree of reliability.
>

The config file solution is not suitable, I think. This code should
run in a general cross platform application (Texttest), and we do not
want each user of that system to be forced to edit a config file..

/Tomas

[toc] | [prev] | [next] | [standalone]


#12014

FromRoy Smith <roy@panix.com>
Date2011-08-22 07:37 -0400
Message-ID<roy-1D7E7C.07373122082011@news.panix.com>
In reply to#12000
In article 
<c4163eeb-c306-49d1-9337-0eb9f47df52f@s12g2000yqm.googlegroups.com>,
 Tomas Lidén <tomas.liden.privat@gmail.com> wrote:

> In this particular case we have a host with several connections (LAN,
> WIFI, VmWare adapters etc). When using gethostbyname() we got a VmWare
> adapter but we wanted to get the LAN (or the "best" connection to our
> server).

Figuring out which is the best connection is a decidedly non-trivial 
problem.  You could try some heuristic like "pick the address which has 
the same network number as me".  But, what if you're multi-homed as 
well, and the target has addresses on more than one of the networks 
you're connected to?  Not to mention that even enumerating all of your 
own connections is non-trivial.

> With gethostbyname_ex() the ordering seemed to become LAN,
> Wifi, etc and that's good for us. But we don't know if this holds on
> other platforms (we're running on Windows 7).

You can't count on the ordering being anything in particular.  It's a 
lost cause.  You may think you can find patterns, and then write your 
application to depend on those, and you will eventually get burned.

> A cross-platform deterministic order would be excellent for us.

"A cross-platform deterministic X would be excellent" is a true 
statement for almost any value of X.  Many people have wasted much of 
their lives trying to achieve that goal, for various Xs.

[toc] | [prev] | [next] | [standalone]


#12019

FromPaul Kölle <paul@subsignal.org>
Date2011-08-22 14:18 +0200
Message-ID<mailman.311.1314015514.27778.python-list@python.org>
In reply to#12014
Am 22.08.2011 13:37, schrieb Roy Smith:
> In article
> <c4163eeb-c306-49d1-9337-0eb9f47df52f@s12g2000yqm.googlegroups.com>,
>   Tomas Lidén<tomas.liden.privat@gmail.com>  wrote:
>> A cross-platform deterministic order would be excellent for us.
>
> "A cross-platform deterministic X would be excellent" is a true
> statement for almost any value of X.  Many people have wasted much of
> their lives trying to achieve that goal, for various Xs.
So true..., QOTW +1

[toc] | [prev] | [next] | [standalone]


#12020

FromTomas Lidén <tomas.liden.privat@gmail.com>
Date2011-08-22 05:33 -0700
Message-ID<fe81c99c-1615-4300-b9e6-392695f9b3ee@br5g2000vbb.googlegroups.com>
In reply to#12019
OK - thank you all for your contributions.
/T

[toc] | [prev] | [next] | [standalone]


#12037

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-08-23 02:09 +1000
Message-ID<4e527f4e$0$29996$c3e8da3$5496439d@news.astraweb.com>
In reply to#12000
Tomas Lidén wrote:


> In this particular case we have a host with several connections (LAN,
> WIFI, VmWare adapters etc). When using gethostbyname() we got a VmWare
> adapter but we wanted to get the LAN (or the "best" connection to our
> server). 

Define "best" connection.

If I tell you that my server has the following 6 connections:

Wifi1, LAN4, LAN1, LAN2, Wifi2, LAN5

which one is "best"?

Once you have an algorithm for deciding which connection is "best" for
everybody, then you can check whether gethostbyname_ex uses that algorithm,
or some other one.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#12010

FromRoy Smith <roy@panix.com>
Date2011-08-22 07:26 -0400
Message-ID<roy-44FF9C.07265622082011@news.panix.com>
In reply to#11997
In article 
<356978ef-e9c1-48fd-bb87-849fe8e27a79@p5g2000vbl.googlegroups.com>,
 Tomas Lidén <tomas@liden-privat.net> wrote:

> In what order are the addresses returned by socket.gethostbyname_ex()?
> 
> We know that gethostbyname() is indeterministic but hope that
> gethostbyname_ex() has a specified order.

Why would you hope that?  Or maybe a better question is, why would you 
expect that?  In general, all name resolution calls return results in 
arbitrary order.  In some cases, results are intentionally changed on 
every call (i.e. round-robin) in an attempt at load sharing.

What kind of ordering were you hoping for?

[toc] | [prev] | [next] | [standalone]


#12015

FromTomas Lidén <tomas.liden.privat@gmail.com>
Date2011-08-22 04:39 -0700
Message-ID<034ff4bf-e3e4-47ff-9a6c-195412431e73@s20g2000yql.googlegroups.com>
In reply to#12010
On 22 Aug, 13:26, Roy Smith <r...@panix.com> wrote:
> In article
> <356978ef-e9c1-48fd-bb87-849fe8e27...@p5g2000vbl.googlegroups.com>,
>  Tomas Lidén <to...@liden-privat.net> wrote:
>
> > In what order are the addresses returned by socket.gethostbyname_ex()?
>
> > We know that gethostbyname() is indeterministic but hope that
> > gethostbyname_ex() has a specified order.
>
> Why would you hope that?  Or maybe a better question is, why would you
> expect that?  In general, all name resolution calls return results in
> arbitrary order.  In some cases, results are intentionally changed on
> every call (i.e. round-robin) in an attempt at load sharing.
>
> What kind of ordering were you hoping for?

See previous posts.

Basically I was asking about the contract for this method.. hoping
that it is deterministic.
Our testing indicated that the interfaces are returned in a specific
order, but we want to know if this is really the case (on all
platforms).

/Tomas

[toc] | [prev] | [next] | [standalone]


#12018

FromRoy Smith <roy@panix.com>
Date2011-08-22 08:14 -0400
Message-ID<roy-2D2D70.08141022082011@news.panix.com>
In reply to#12015
In article 
<034ff4bf-e3e4-47ff-9a6c-195412431e73@s20g2000yql.googlegroups.com>,
 Tomas Lidén <tomas.liden.privat@gmail.com> wrote:

> Basically I was asking about the contract for this method.. hoping
> that it is deterministic.

The contract for socket.gethostbyname_ex() is described at 
http://docs.python.org/library/socket.html#socket.gethostbyname_ex.  It 
says:

"Translate a host name to IPv4 address format, extended interface. 
Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the 
primary host name responding to the given ip_address, aliaslist is a 
(possibly empty) list of alternative host names for the same address, 
and ipaddrlist is a list of IPv4 addresses for the same interface on the 
same host (often but not always a single address). gethostbyname_ex() 
does not support IPv6 name resolution, and getaddrinfo() should be used 
instead for IPv4/v6 dual stack support."

That's it.  It says nothing about ordering, so nothing about ordering 
should be inferred.

> Our testing indicated that the interfaces are returned in a specific
> order, but we want to know if this is really the case (on all
> platforms).

No, it is not.

[toc] | [prev] | [next] | [standalone]


#12041

FromTerry Reedy <tjreedy@udel.edu>
Date2011-08-22 13:34 -0400
Message-ID<mailman.323.1314034539.27778.python-list@python.org>
In reply to#12015
On 8/22/2011 7:39 AM, Tomas Lidén wrote:
> On 22 Aug, 13:26, Roy Smith<r...@panix.com>  wrote:
>> In article
>> <356978ef-e9c1-48fd-bb87-849fe8e27...@p5g2000vbl.googlegroups.com>,
>>   Tomas Lidén<to...@liden-privat.net>  wrote:
>>
>>> In what order are the addresses returned by socket.gethostbyname_ex()?
>>
>>> We know that gethostbyname() is indeterministic but hope that
>>> gethostbyname_ex() has a specified order.
>>
>> Why would you hope that?  Or maybe a better question is, why would you
>> expect that?  In general, all name resolution calls return results in
>> arbitrary order.  In some cases, results are intentionally changed on
>> every call (i.e. round-robin) in an attempt at load sharing.
>>
>> What kind of ordering were you hoping for?
>
> See previous posts.
>
> Basically I was asking about the contract for this method..

The doc "Return a triple (hostname, aliaslist, ipaddrlist) where 
hostname is the primary host name responding to the given ip_address, 
aliaslist is a (possibly empty) list of alternative host names for the 
same address, and ipaddrlist is a list of IPv4/v6 addresses for the same 
interface on the same host (most likely containing only a single address)."

> hoping that it is deterministic.

As far as I can see, that is not in the contract.

> Our testing indicated that the interfaces are returned in a specific
> order, but we want to know if this is really the case (on all
> platforms).

Even if it were so now, a patch could change things.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#12049

FromNobody <nobody@nowhere.com>
Date2011-08-22 20:12 +0100
Message-ID<pan.2011.08.22.19.12.15.775000@nowhere.com>
In reply to#11997
On Sun, 21 Aug 2011 23:37:42 -0700, Tomas Lidén wrote:

> In what order are the addresses returned by socket.gethostbyname_ex()?
> 
> We know that gethostbyname() is indeterministic but hope that
> gethostbyname_ex() has a specified order.

It doesn't. In fact, the order of the IP addresses may have been
deliberately randomised (by the resolver and/or DNS server) in order to
provide a simple form of load balancing.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web