Getting local IP address...

arhodes at psionic.com.bbs arhodes at psionic.com.bbs
Mon Jul 17 14:10:02 EDT 2000


Howdy...

That works too.  There are some purists
who may take offense to reliance on external system binaries
(ifconfig and grep)...not a big deal in my opinion.
ifconfig on Linux probably does something similar
to the fcntl/ioctl stuff I was mentioning...perhaps there
should be a platform independent python-ifconfig library?

The API to such a library could be constructed to provide
a standard way to do "ifconfig-ish" stuff on
Windows/NT/Unices. (i.e. up and down network interfaces,
get the ip address off the net interface, set the ip on the
net interface, change firewall rules perhaps, etc.)

Aaron
arhodes at psionic.com


Tony Johnson wrote:
>
> Why not just do an addr = os.system(ifconfig eth0 | grep inet)
> This should return the ip addy of the nic...
>
> Since the way OS's store IP addys is so implementaion dependant, I don't
> see how it could be portable unless people start making a posix way of
> storing an ip addy on top of the os dependant way
>
> On Monday, July 17, 2000 11:33 AM, Aaron Rhodes [SMTP:arhodes at psionic.com]
> wrote:
> > Here's a tremendously non-portable way to do it for
> > Linux by reading it straight off the ethernet card...
> >
> > import string,socket,fcntl
> > SIOCGIFADDR = 0x8915
> > ifname = 'eth0'
> > s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
> > ifr = ifname+'\0'*(16-len(ifname))+chr(socket.AF_INET)+15*'\0'
> > r= fcntl.ioctl(s.fileno(),SIOCGIFADDR,ifr)
> > addr = string.join(map(str,map(ord,r[20:24])),'.')
> > print addr
> >
> >



More information about the Python-list mailing list