How to get IP address
Nagy László Zsolt
nagylzs at freemail.hu
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', , ['22.214.171.124', '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'])
('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:
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
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
that is used to connect to a given address and this is sufficient in
Any comments from Python experts? Did I missed something?
More information about the Python-list