I stumbled on this a while back. It may only work on Linux because of
the fcntl.
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
>>> get_ip_address('lo')
'127.0.0.1'
>>> get_ip_address('eth0')
'38.113.228.130'
Ty wrote:
> I'd like to grab the users external IP address. I'm using it for a
> spam catching class that I'm porting over to Django/Python. Is this
> possible?
>
> This person outlines how to 'hack' it, but I'd rather not depend on an
> external site.
> http://dmiessler.com/blog/how-to-use-python-to-get-your-external-ip
> >
>
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com
> Version: 8.0.173 / Virus Database: 270.8.1/1730 - Release Date: 10/17/2008
> 8:07 AM
>
--
Jeff
Jeff Johnson
[EMAIL PROTECTED]
Phoenix Python User Group - [EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---