How to get IP address

Nagy László Zsolt nagylzs at freemail.hu
Mon Mar 3 07:06:19 EST 2003


> Opps, too quick :)
>
> OK.  Running
>
> socket.gethostbyaddr( socket.gethostname() )
>
> will return all your IP's, internal and external.  For example:
>
> ('hallows', [], ['101.101.101.102', '10.253.1.118'])
>
>
> 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', [], ['192.168.1.1', 'x.x.x.x'])


Returns this:

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

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:

192.168.1.1                    'hostname1'
192.168.1.2                    'hostname1'
10.0.0.1                          'hostname2'

When you call gethostbyaddr('hostname1'), you will get addresses for 
'hostname1' only:  ['192.168.1.1','192.168.1.2']
['10.0.0.1'] 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