How to get IP address

Nagy László Zsolt nagylzs at
Mon Mar 3 13:06:19 CET 2003

> Opps, too quick :)
> OK.  Running
> socket.gethostbyaddr( socket.gethostname() )
> will return all your IP's, internal and external.  For example:
> ('hallows', [], ['', ''])
> is the output of my own, multi-homed machine.  "Hallows" is my
> hostname/computer name, with the 2nd list showing the IP's from my two
> devices ( ethernet, and modem).  As a matter of interest, what is the
> output of gethostbyaddr?  It should look something like:
> ('arwen', [], ['', 'x.x.x.x'])

Returns this:

  ('arwen', ['arwen.msnet'], [''])

Really, that computer has two IP addresses, but the other (not fix) 
address has no (fix) host name associated.
Consider a machine with several (fix) IP addresses:                    'hostname1'                    'hostname1'                          'hostname2'

When you call gethostbyaddr('hostname1'), you will get addresses for 
'hostname1' only:  ['','']
[''] is returned when you call gethostbyaddr('hostname2').
I wanted to get the IP address in ANY case. Maybe I don't have a DNS 
name at all.
You can't use gethostbyaddr() if you don't know IP address neither a DNS 
name. ;-)

It seems to me that there is no way to get all IP addresses of the 
machine where your
Python program is running. The standard library does not provide a function
to do this. Although there is a way to get one of the IP addresses on 
the interface
that is used to connect to a given address and this is sufficient in 
most cases.
Any comments from Python experts? Did I missed something?

  Laci 1.0

More information about the Python-list mailing list