0

I think the question says it all. I want a way to discover the ip address of the local interfaces in python.

I have seen lots of variants on this question (such as this one, but it seems that none of them quite cover ALL of the bases. (the one I cite, for instance, asks how to find "the" local IP address, not all of them).

To sum up the requirements:

  1. I want it to be reliably cross platform
  2. I want it to require NO additional libraries (I know about netifaces, for instance)
  3. I want it to discover ALL of the local interfaces, not just some
  4. No packets should have to be sent out of any interface.

That's being a bit demanding, I realize :-). The point is this is a common question that comes up in a variety of situations, and there should, IMHO, be a "copy and paste" solution (i.e. a snippet I can put in my code to provide this functionality cross platform).

EDIT

For reference, there's also this question, which presumes the sending of packets.

Community
  • 1
  • 1
stochastic
  • 3,155
  • 5
  • 27
  • 42
  • 3
    If you could do such a thing within vanilla python, I don't think he would've gone to the trouble of making netifaces... – antlersoft Apr 24 '13 at 20:48
  • @antlersoft I can see where you're coming from. I'm pretty sure there's no SIMPLE way to do this with vanilla python. What I envision is a code snippet that uses several methods, catching errors as appropriate. Probably long and nasty, but cut-and-paste-able. The fact that netifaces sortof does this (website doesn't say if it works on Windows and I haven't tried) is what leads me to believe that it is possible. The fact is, the kernel of every OS has this information. There has **got** to be a way, it seems to me, to do something as simple as enumerating addresses. – stochastic Apr 24 '13 at 21:00
  • Unless I'm mistaken, netifaces uses a native C library to provide this functionality, which uses platform-specifc system calls. Because Python is designed to abstract away those platform details, it would be quite a chore to try to duplicate all the platform-logic contained in his #ifdef's – antlersoft Apr 24 '13 at 21:07
  • @antlersoft I don't deny that it would be a chore, but it seems like it could be done. I'm thinking of using fcntl, for instance (which was involved in one way in the threads I mentioned), seeing if/to what extent that works, then maybe using the gethostbyname solutions, taking the union of that information, etc... – stochastic Apr 24 '13 at 21:24
  • If you throw out your "reliably cross-platform" requirement, of course, you can do it somewhat more easily :). – antlersoft Apr 24 '13 at 21:37
  • Just to chip in, since I wrote netifaces, @antlersoft is right that the *entire point* of netifaces was to solve this problem. It doesn’t use any libraries other than standard system ones either, so it really is just a case of installing and using it, and it's *supposed* to be reliably cross-platform (it's been used on OS X, Windows, Linux, Solaris and FreeBSD *at least*). – al45tair May 02 '14 at 15:53
  • @alastair Thanks for chipping in. The problem I have with netifaces is mostly just that it isn't standard python, and thus I have to ensure it's installed before I use it... yet another dependency. Installing with pip is fast, but the verification was almost nonexistent last time I checked, and security issues like that matter sometimes :-). Any chance of getting this into standard python (I know, or rather have heard, that PEPs are quite a lot of work, but...). Thanks for the great package at any rate! – stochastic May 02 '14 at 19:16
  • @stochastic TBH I haven't really thought about proposing its inclusion in the standard library. I know there have been improvements in package verification over the past couple of years (e.g. there are MD5 sums and packages need to be downloaded from PyPI unless you pass some special flags to pip). – al45tair May 04 '14 at 13:15
  • @stochastic One other thought does occur to me---if you wanted, you could grab the code from netifaces and put it directly into your source tree. No pip necessary, in that case, and you've got control over exactly which version of the netifaces code you're using. – al45tair May 04 '14 at 13:18

0 Answers0