Portable (Linux/Mac/Win) way to get network interfaces names andtheir addresses?

Discussion in 'Python' started by Alessio Pace, Jan 7, 2009.

  1. Alessio Pace

    Alessio Pace Guest

    Hi,

    I'm wondering how could I get, possibly in a pure Python solution, the
    list of network addresses on a machine and the IP address of each of
    them.

    In fact I came across recently on two solutions, one that is pure
    Python but that works only on Linux:

    #############################
    def all_interfaces():
    max_possible = 128 # arbitrary. raise if needed.
    bytes = max_possible * 32
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    names = array.array('B', '\0' * bytes)
    outbytes = struct.unpack('iL', fcntl.ioctl(
    s.fileno(),
    0x8912, # SIOCGIFCONF
    struct.pack('iL', bytes, names.buffer_info()[0])
    ))[0]
    namestr = names.tostring()
    return [namestr[i:i+32].split('\0', 1)[0] for i in range(0,
    outbytes, 32)]

    def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
    s.fileno(),
    0x8915, # SIOCGIFADDR
    struct.pack('256s', ifname[:15])
    )[20:24])
    ###########################

    and one other instead that is in the "netifaces" package (=>
    http://alastairs-place.net/netifaces/) which is written in C.


    Thanks in advance for any suggestion.
    --
    Alessio Pace.
    Alessio Pace, Jan 7, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ARaman
    Replies:
    1
    Views:
    381
    Mike Wahler
    Oct 23, 2003
  2. Christian  Decker

    Portable libraries for Mac/Win/Lin?

    Christian Decker, Oct 19, 2004, in forum: C++
    Replies:
    3
    Views:
    320
    Mike Wahler
    Oct 19, 2004
  3. namespace1
    Replies:
    3
    Views:
    875
  4. Krist
    Replies:
    6
    Views:
    699
    Arne Vajhøj
    May 7, 2010
  5. Theodore Knab
    Replies:
    1
    Views:
    427
    Robert Klemme
    Feb 4, 2004
Loading...

Share This Page