finding IP address of computer

Discussion in 'Python' started by Chris, Apr 27, 2006.

  1. Chris

    Chris Guest

    How do I find and print to screen the IP address of the computer my
    python program is working on?
     
    Chris, Apr 27, 2006
    #1
    1. Advertising

  2. Chris schrieb:
    > How do I find and print to screen the IP address of the computer my
    > python program is working on?
    >


    IP adresses are bound to network interfaces not to computers.
    One Computer can have multiple network interfaces.

    --
    Servus, Gregor
    http://www.gregor-horvath.com
     
    Gregor Horvath, Apr 27, 2006
    #2
    1. Advertising

  3. One way:

    >>> import socket
    >>> socket.getaddrinfo(socket.gethostname(), None)[0][4][0]


    It was the first google hit
     
    BartlebyScrivener, Apr 27, 2006
    #3
  4. Chris

    Chris Guest

    hehe, works a charm, cheers mate.
     
    Chris, Apr 27, 2006
    #4
  5. Chris

    Jorge Godoy Guest

    Chris wrote:

    > hehe, works a charm, cheers mate.


    Beware that if you have a different entry in your hosts file you can match a
    different name.

    Test it:

    - add "127.0.0.2 yourhost.yourdomain yourhost" to /etc/hosts
    - rerun the code.

    You'll see "127.0.0.2" as the result. So take that into account.

    --
    Jorge Godoy <>

    "Quidquid latine dictum sit, altum sonatur."
    - Qualquer coisa dita em latim soa profundo.
    - Anything said in Latin sounds smart.
     
    Jorge Godoy, Apr 27, 2006
    #5
  6. Chris

    sturlamolden Guest

    print '127.0.0.1'

    :p
     
    sturlamolden, Apr 27, 2006
    #6
  7. On 2006-04-27, Gregor Horvath <> wrote:
    > Chris schrieb:
    >> How do I find and print to screen the IP address of the computer my
    >> python program is working on?
    >>

    >
    > IP adresses are bound to network interfaces not to computers.
    > One Computer can have multiple network interfaces.


    And each interface can have any number if IP addresses
    (including none).

    --
    Grant Edwards grante Yow! I feel like a wet
    at parking meter on Darvon!
    visi.com
     
    Grant Edwards, Apr 27, 2006
    #7
  8. Chris

    Terry Reedy Guest

    "Grant Edwards" <> wrote in message
    news:...
    > On 2006-04-27, Gregor Horvath <> wrote:
    >> Chris schrieb:
    >>> How do I find and print to screen the IP address of the computer my
    >>> python program is working on?
    >>>

    >>
    >> IP adresses are bound to network interfaces not to computers.
    >> One Computer can have multiple network interfaces.

    >
    > And each interface can have any number if IP addresses
    > (including none).


    To answer the OP for typical situations: if you are accessing the internet
    via a local network, the network administrator should be able to tell you.
    In fact, for some networks, the IP address is part of the interface setup.
    If the network is run by a router, it should be able to tell you. If you
    are sitting behind a NAT (network address translation) router and you want
    to know the external address, there are web pages that echo your externally
    visible address back to you.

    Terry Jan Reedy
     
    Terry Reedy, Apr 27, 2006
    #8
  9. Chris

    DarkBlue Guest

    Chris wrote:

    > How do I find and print to screen the IP address of the computer my
    > python program is working on?


    def readip():
    import re, urllib
    f = urllib.urlopen('http://checkip.dyndns.org')
    s = f.read()
    m = re.search('([\d]*\.[\d]*\.[\d]*\.[\d]*)', s)
    return m.group(0)

    myip = readip()
     
    DarkBlue, Apr 28, 2006
    #9
  10. Chris

    Paul Watson Guest

    DarkBlue wrote:
    > Chris wrote:
    >
    >
    >>How do I find and print to screen the IP address of the computer my
    >>python program is working on?

    >
    >
    > def readip():
    > import re, urllib
    > f = urllib.urlopen('http://checkip.dyndns.org')
    > s = f.read()
    > m = re.search('([\d]*\.[\d]*\.[\d]*\.[\d]*)', s)
    > return m.group(0)
    >
    > myip = readip()


    IP address and other browser header information available at:

    http://xhaus.com/headers
     
    Paul Watson, May 1, 2006
    #10
    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. A. M. G. Solo
    Replies:
    0
    Views:
    590
    A. M. G. Solo
    Jan 13, 2006
  2. A. M. G. Solo
    Replies:
    0
    Views:
    825
    A. M. G. Solo
    Jun 8, 2006
  3. Replies:
    0
    Views:
    366
  4. A. M. G. Solo (do not reply to this email address)
    Replies:
    0
    Views:
    751
    A. M. G. Solo (do not reply to this email address)
    Nov 8, 2006
  5. A. M. G. Solo
    Replies:
    0
    Views:
    776
    A. M. G. Solo
    Dec 5, 2006
Loading...

Share This Page