How to get IP address

Wayne Pierce wayne at mishre.com
Tue Mar 4 05:43:01 EST 2003


Nagy László Zsolt <nagylzs at freemail.hu> wrote in message news:<mailman.1046693203.18366.python-list at python.org>...
> > 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'])

What command did you type to get this output?  Did you use:

socket.gethostbyaddr( socket.gethostname() )

Or something else?  If you did not type the line above, what does it
return?

> When you call gethostbyaddr('hostname1'), you will get addresses for 
> 'hostname1' only:  ['192.168.1.1','192.168.1.2']

Exactly, you are specifying that you only want to see the data for the
specified hostname.  If you do not put a specific hostname and use
socket.gethostname() it should give you the info for everything.

> You can't use gethostbyaddr() if you don't know IP address neither a DNS 
> name. ;-)

Yes, you can.  gethostbyaddr() can be pointed to the local system in a
couple of different ways without an IP or DNS name, you could use
'localhost' and gethostname().

-W




More information about the Python-list mailing list