Error in simple python module
Question: I tried to write a little python script today but have failed horribly. Why is
it that the code below gives me the following error after being called from
the shell?

Error

    
    
    File "./testmod.py", line 15, in <module>
        printdnsfile(sys.argv[1])
      File "./testmod.py", line 10, in printdnsfile
        print(socket.gethostbyname(str(line)))
    socket.gaierror: [Errno 8] nodename nor servname provided, or not known
    

Code

    
    
    #!/usr/bin/python
    
    def printdnsfile(file):
        file= open (file,"r")
        import socket
        dest = open("/dnsfile.txt",'w')
        for line in file:
            print(socket.gethostbyname(str(line)))
            print>>dest, str(",".join([line,socket.gethostbyname(line)])+'\n')
    
    if __name__ == "__main__":
        import sys
        printdnsfile(sys.argv[1]) 
    

I tested the socket module in the python-console and it worked as expected. Is
there an error with my code or is this a problem with my configuration?

Thanks.


Answer: You might have an empty line in your input file. Try checking your line before
you gethostbyname.

    
    
    def printdnsfile(file):
        file= open (file,"r")
        import socket
        dest = open("/dnsfile.txt",'w')
        for line in file:
            line = line.strip()
            if line:
                print(socket.gethostbyname(str(line)))
                print>>dest, str(",".join([line,socket.gethostbyname(line)])+'\n')
    

