Determine ip address

Donn Cave donn at u.washington.edu
Fri Apr 15 11:43:24 EDT 2005


In article <mailman.1959.1113571156.1799.python-list at python.org>,
 Simon Brunning <simon.brunning at gmail.com> wrote:

> On 15 Apr 2005 06:03:06 -0700, codecraig <codecraig at gmail.com> wrote:
> > hi,
> >    how can i use python to figure the ip address of the machine which
> > the python script is running on?  I dont mean like 127.0.0.1....but i
> > want the external IP address (such as ipconfig on windows displays).
> 
> On Windows, this works:
> 
> socket.gethostbyname(socket.gethostname())
> 
> Is that OK on real operating systems too?

It will work sometimes, but there is nothing I know of
that specifically distinguishes "the" external network.
If you want something that reliably finds a network that
will be used for a certain type of connection, then the
best thing to do is make a connection like that, and
inspect the results.  The getsockname() method shows the
IP address.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list