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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to