Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29387

Re: using text file to get ip address from hostname

From Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Newsgroups comp.lang.python
Subject Re: using text file to get ip address from hostname
Date 2012-09-17 22:37 +0200
Organization A newly installed InterNetNews server
Message-ID <k381lp$fut$1@r03.glglgl.gl> (permalink)
References <e4e100d1-bdf9-45d0-a137-615b1e820cfb@googlegroups.com> <0c93c99d-c837-4b20-b78f-8c367ad3cdae@googlegroups.com>

Show all headers | View raw


Am 15.09.2012 18:20 schrieb Dan Katorza:

> hello again friends,
> thanks for everyone help on this.
> i guess i figured it out in two ways.
> the second one i prefer the most.
>
> i will appreciate if someone can give me some tips.
> thanks again
>
> so...
> -------------------------------------------------------------
> First
> -------------------------------------------------------------
> #!/usr/bin/env python
> #Get the IP Address
>
>
> print("hello, please enter file name here>"),
> import socket
> for line in open(raw_input()):
>      hostname = line.strip()
>      print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostname)))
>
> ------------------------------------------------------------
> second
> ------------------------------------------------------------
> #!/usr/bin/env python
> #Get the IP Address
>
> import os
>
> print("Hello, please enter file name here>"),
> FILENAME = raw_input()
> if os.path.isfile(FILENAME):
>      print("\nFile Exist!")
>      print("\nGetting ip from host name")
>      print("\n")
>      import socket
>      for line in open (FILENAME):
>          hostname = line.strip()
>          print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostname)))
>      else:
>          print ("\nFinished the operation")
> else:
>      print ("\nFIle is missing or is not reasable"),
> ~

Comparing these, the first one wins if you catch and process exceptions. 
It is easier to ask for forgiveness than to get permission (EAFP, 
http://en.wikipedia.org/wiki/EAFP).

Bit I wonder that no one has mentionned that 
socket.gethostbyname(hostname) is quite old-age because it only returns 
IPv4 addresses (resp. only one of them).

OTOH, socket.getaddrinfo(hostname, 0, 0, socket.SOCK_STREAM) gives you a 
list of parameter tuples for connecting.

So which way you go above, you should change the respective lines to

for line in ...:
     hostname = line.strip()
     for target in socket.getaddrinfo(hostname, 0, socket.AF_UNSPEC,
             socket.SOCK_STREAM):
         print("IP address for {0} is {1}.".format(hostname,
             target[4][0]))


Thomas

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

using text file to get ip address from hostname dkatorza@gmail.com - 2012-09-12 07:24 -0700
  Re: using text file to get ip address from hostname Chris Angelico <rosuav@gmail.com> - 2012-09-13 00:35 +1000
  Re: using text file to get ip address from hostname dkatorza@gmail.com - 2012-09-12 07:41 -0700
    Re: using text file to get ip address from hostname Alister <alister.ware@ntlworld.com> - 2012-09-12 17:51 +0000
    Re: using text file to get ip address from hostname Terry Reedy <tjreedy@udel.edu> - 2012-09-12 17:04 -0400
  Re: using text file to get ip address from hostname Jason Friedman <jason@powerpull.net> - 2012-09-12 21:12 -0600
  Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-15 09:20 -0700
    Re: using text file to get ip address from hostname Hans Mulder <hansmu@xs4all.nl> - 2012-09-15 20:52 +0200
    Re: using text file to get ip address from hostname Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-17 22:37 +0200
  Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-15 15:43 -0700
    Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 00:41 -0700
      Re: using text file to get ip address from hostname Chris Angelico <rosuav@gmail.com> - 2012-09-19 18:14 +1000
        Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 01:50 -0700
          Re: using text file to get ip address from hostname Chris Angelico <rosuav@gmail.com> - 2012-09-19 18:59 +1000
        Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 01:50 -0700
          Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 02:10 -0700
          Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 02:10 -0700
            Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 05:28 -0700
              Re: using text file to get ip address from hostname Dave Angel <d@davea.name> - 2012-09-19 14:22 -0400
            Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 05:28 -0700
              Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 22:13 -0700
              Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 22:13 -0700

csiph-web