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


Groups > comp.lang.python > #12054

Re: Order of addresses returned by socket.gethostbyname_ex()

Date 2011-08-23 08:46 +1000
From Cameron Simpson <cs@zip.com.au>
Subject Re: Order of addresses returned by socket.gethostbyname_ex()
References <bed10d56-f5dc-4ffc-b42b-1eeec5d6c619@w11g2000vbp.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.331.1314053201.27778.python-list@python.org> (permalink)

Show all headers | View raw


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>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web