Getting local IP address...

Aaron Rhodes arhodes at psionic.com
Mon Jul 17 12:33:18 EDT 2000


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.



More information about the Python-list mailing list