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

Dan Grassi Dan at Grassi.com
Sat Aug 14 22:47:07 CEST 1999


Yes, it does work on a Mac.

Dan, Dan at Grassi.com

----------
In article <2EZr3.1342$EG4.232940 at news.shore.net>, "Michael P. Reilly"
<arcege at shore.net> wrote:


> don't have
> access to a Mac; but theoretically it should be.
>
>   import socket
>   def my_ipaddr(interface_hostname=None):
>     # give the hostname of the interface you want the ipaddr of
>     hostname = interface_hostname or socket.gethostname()
>     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>     s.bind(hostname, 0)
>     ipaddr, port = s.getsockname()
>     s.close()
>     return ipaddr  # returns 'nnn.nnn.nnn.nnn' (StringType)




More information about the Python-list mailing list