Order of addresses returned by socket.gethostbyname_ex()

Cameron Simpson cs at zip.com.au
Mon Aug 22 06:06:46 EDT 2011

On 22Aug2011 02:06, Tomas Lid�n <tomas.liden.privat at gmail.com> wrote:
| On 22 Aug, 10:15, Steven D'Aprano <steve
| +comp.lang.pyt... at 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"

Cameron Simpson <cs at zip.com.au> DoD#743

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

More information about the Python-list mailing list