Determine ip address

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • codecraig

    Determine ip address

    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....bu t i
    want the external IP address (such as ipconfig on windows displays).

    any ideas??

    THanks

  • Simon Brunning

    #2
    Re: Determine ip address

    On 15 Apr 2005 06:03:06 -0700, codecraig <codecraig@gmai l.com> wrote:[color=blue]
    > 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....bu t i
    > want the external IP address (such as ipconfig on windows displays).[/color]

    On Windows, this works:

    socket.gethostb yname(socket.ge thostname())

    Is that OK on real operating systems too?

    --
    Cheers,
    Simon B,
    simon@brunningo nline.net,

    Comment

    • rbt

      #3
      Re: Determine ip address

      codecraig wrote:[color=blue]
      > 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....bu t i
      > want the external IP address (such as ipconfig on windows displays).
      >
      > any ideas??
      >
      > THanks
      >[/color]

      To get the public IP (like when you're behind a wirless router, etc) you
      may try doing something like this:

      import urllib
      import re
      import time

      ip_search = re.compile ('\d{1,3}\.\d{1 ,3}\.\d{1,3}\.\ d{1,3}')

      try:
      f = urllib.urlopen( "http://www.ipchicken.c om")
      data = f.read()
      f.close()
      current_ip = ip_search.finda ll(data)
      if current_ip:
      print current_ip
      time.sleep(3)
      except Exception:
      pass

      HTH,

      rbt

      Comment

      • Andy Jeffries

        #4
        Re: Determine ip address

        codecraig wrote:[color=blue]
        > 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....bu t i
        > want the external IP address (such as ipconfig on windows displays).[/color]

        I use the following (all on one line):

        external_ip = os.popen("/sbin/ifconfig eth0|/bin/grep inet|/bin/awk
        '{print $2}' | sed -e s/.*://", "r").read().str ip()

        Cheers,


        Andy

        Comment

        • Donn Cave

          #5
          Re: Determine ip address

          In article <mailman.1959.1 113571156.1799. python-list@python.org >,
          Simon Brunning <simon.brunning @gmail.com> wrote:
          [color=blue]
          > On 15 Apr 2005 06:03:06 -0700, codecraig <codecraig@gmai l.com> wrote:[color=green]
          > > 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....bu t i
          > > want the external IP address (such as ipconfig on windows displays).[/color]
          >
          > On Windows, this works:
          >
          > socket.gethostb yname(socket.ge thostname())
          >
          > Is that OK on real operating systems too?[/color]

          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@u.washingt on.edu

          Comment

          • Lee Harr

            #6
            Re: Determine ip address

            On 2005-04-15, codecraig <codecraig@gmai l.com> wrote:[color=blue]
            > 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....bu t i
            > want the external IP address (such as ipconfig on windows displays).
            >
            > any ideas??
            >[/color]



            I use this:

            #conf.py
            ifconfig = '/sbin/ifconfig'
            iface = 'eth0'
            telltale = 'inet addr:'


            #addr.py
            import commands

            from conf import ifconfig, iface, telltale

            def my_addr():
            cmd = '%s %s' % (ifconfig, iface)
            output = commands.getout put(cmd)

            inet = output.find(tel ltale)
            if inet >= 0:
            start = inet + len(telltale)
            end = output.find(' ', start)
            addr = output[start:end]
            else:
            addr = ''

            Comment

            • fuzzylollipop

              #7
              Re: Determine ip address

              import socket
              print socket.gethostb yname( socket.gethostn ame() )

              Comment

              • ontiscal@tenup.com

                #8
                Re: Determine ip address



                codecraig ha scritto:[color=blue]
                > 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....bu t i
                > want the external IP address (such as ipconfig on windows displays).
                >
                > any ideas??
                >
                > THanks[/color]

                Comment

                • Mike Meyer

                  #9
                  Re: Determine ip address

                  [Format recovered from top-posting.]

                  ontiscal@tenup. com writes:[color=blue]
                  > codecraig ha scritto:[color=green]
                  >> 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....bu t i
                  >> want the external IP address (such as ipconfig on windows displays).[/color]
                  >
                  > http://checkip.tk/[/color]

                  That won't work if you're on a NAT'ed network - it will instead return
                  the external address of the NAT gateway. ipconfig displays the ip
                  address(es) of the interfaces on the current machine.

                  You need to use the socket.socket.g etsockname method:

                  py> import socket
                  py> s = socket.socket()
                  py> s.connect(("goo gle.com", 80))
                  py> s.getsockname()
                  ('192.168.1.1', 57581)
                  py>

                  The local ethernet card is 192.168.1.1, the local port for the socket
                  is 57581.

                  Note that the presence of multiple interface cards makes the question
                  ambiguous.

                  <mike
                  --
                  Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                  Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                  Comment

                  Working...