What's a good way to get my IP from Python?

Charles G Waldman cgw at fnal.gov
Tue Aug 10 21:51:25 CEST 1999


A correction to the code I posted earlier:  change

    ifr = ifname+'\0'*(32-len(ifname))

to

    ifr = ifname+'\0'*(16-len(ifname))+chr(socket.AF_INET)+15*'\0'


As Aaron Rhodes pointed out to me, the version I originally posted
won't work on Linux 2.0 kernels.  It seems that the SIOCGIFADDR ioctl
wants to have the address family in the struct ifrequest on older
kernels, whereas kernel 2.2 doesn't care.  With the correction above,
the code I posted will work on either 2.0 or 2.2.  Also, there are no
differences in the struct ifreq, so no "offset tweaking" is necessary.





More information about the Python-list mailing list