Re: determine IP

Discussion in 'Python' started by Ed Leafe, Nov 7, 2004.

  1. Ed Leafe

    Ed Leafe Guest

    On Nov 7, 2004, at 11:53 AM, wrote:

    > The above code will _always_ return the local IP address, regardless
    > of NAT configuration. For example, running on my desktop:
    >
    >>>> import socket
    >>>> s = socket.socket()
    >>>> s.connect(('google.com', 80))
    >>>> print s.getsockname()

    > ('192.168.42.4', 40044)
    >>>>
    >>>>


    Not OMM:

    >>> import socket
    >>> s = socket.socket()
    >>> s.connect(('google.com', 80))
    >>> s.getsockname()

    ('66.66.222.137', 55096)
    >>>


    The local IP address is in the 192.168 subnet.

    ___/
    /
    __/
    /
    ____/
    Ed Leafe
    http://leafe.com/
    http://dabodev.com/
     
    Ed Leafe, Nov 7, 2004
    #1
    1. Advertising

  2. Ed Leafe

    Michael Fuhr Guest

    Ed Leafe <> writes:

    > On Nov 7, 2004, at 11:53 AM, wrote:
    >
    > > The above code will _always_ return the local IP address, regardless
    > > of NAT configuration. For example, running on my desktop:
    > >
    > >>>> import socket
    > >>>> s = socket.socket()
    > >>>> s.connect(('google.com', 80))
    > >>>> print s.getsockname()

    > > ('192.168.42.4', 40044)

    >
    > Not OMM:
    >
    > >>> import socket
    > >>> s = socket.socket()
    > >>> s.connect(('google.com', 80))
    > >>> s.getsockname()

    > ('66.66.222.137', 55096)
    > >>>

    > The local IP address is in the 192.168 subnet.


    Is your machine dual-homed? getsockname() returns the IP address
    if the interface used for the connection, so if you have multiple
    interfaces (e.g., one outside and one inside) then you'll get the
    one being used. Run that script on a machine whose only interface
    is on the inside network (i.e., a machine that must route through
    the box doing the NAT) and you'll see a private address.

    --
    Michael Fuhr
    http://www.fuhr.org/~mfuhr/
     
    Michael Fuhr, Nov 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anushi
    Replies:
    5
    Views:
    8,997
  2. Sven Heithecker
    Replies:
    2
    Views:
    665
    Ralf Hildebrandt
    Aug 2, 2004
  3. nospam
    Replies:
    2
    Views:
    440
  4. Corny
    Replies:
    1
    Views:
    349
    Natty Gur
    Jul 7, 2003
  5. Peña, Botp
    Replies:
    1
    Views:
    262
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page