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. Advertisements

  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. Advertisements

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:
    10,571
  2. Sven Heithecker
    Replies:
    2
    Views:
    796
    Ralf Hildebrandt
    Aug 2, 2004
  3. nospam
    Replies:
    2
    Views:
    508
  4. Corny
    Replies:
    1
    Views:
    418
    Natty Gur
    Jul 7, 2003
  5. Tim Pacl

    Determine PageLayout programatically

    Tim Pacl, Jul 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    426
    Marina
    Jul 8, 2003
  6. KathyB
    Replies:
    0
    Views:
    364
    KathyB
    Jul 20, 2003
  7. Kian Goh
    Replies:
    2
    Views:
    493
    Steve C. Orr, MCSD
    Jul 24, 2003
  8. Peña, Botp
    Replies:
    1
    Views:
    477
    Robert Klemme
    Jan 24, 2004
Loading...