Order of addresses returned by socket.gethostbyname_ex()

Roy Smith roy at panix.com
Mon Aug 22 13:37:31 CEST 2011


In article 
<c4163eeb-c306-49d1-9337-0eb9f47df52f at s12g2000yqm.googlegroups.com>,
 Tomas Lidén <tomas.liden.privat at 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.



More information about the Python-list mailing list