Getting local IP address...

Tony Johnson gjohnson at gs.verio.net
Mon Jul 17 12:52:36 EDT 2000


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
>
>
> Aaron
> arhodes at psionic.com
>
>
> Stephen Hansen wrote:
> >
> >     Nevermind.
> >
> >     I've discovered i'm intelligent enough to actually chcek Deja and 
get
> > an answer so that i doin't have to spam ya'll with stupid questions :)
> >
> >     Heh. :)
> >
> > --S
> >
> > Stephen Hansen <stephen at cerebralmaelstrom.com> wrote in message
> > news:8ko8qr$7cd$1 at nnrp1.deja.com...
> > > How do I go about getting my dynamic IP address? I want to make a
> > > little program to check periodically (daily or so) and log the 
results
> > > so I can determine how often my IP is getting shuffled. :)
> > >
> > > Thanks :)
> > >
> > > --Stephen
> > >
> > >
> > > Sent via Deja.com http://www.deja.com/
> > > Before you buy.
> --
> http://www.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list