On 12Jul2015 22:36, Nym City <nymc...@yahoo.com> wrote:
Hello,
I am working on a 2 individual programs. In the first program, I am taking in a 
list of domains in .csv format and want to get the associated IPs.
In the second program, I want to import in a list of IP .csv and than get the 
associated domains out.
Eventually I want to get both of the above programs to write out to the same 
.csv file that they are reading from- in the next column. But I am not there 
yet.
Doing research I came across the following 
example:http://python.about.com/od/pythonstandardlibrary/qt/dnscheck.htm
From the link above, doing domain -> IP would be easier to begin with. Here are 
the two variations of my code:

Variation 1:
import csv
import socket

domains = open('top500domains.csv', 'r')
for domain in domains:

You're getting a whole line here, including the traling newline. Add this line:

   print("domain=%r" % (domain,))

at the start of the loop to see this.

    domain = socket.gethostbyname(str(domains))

There's no point converting a line to str; a line is a str. If you're wokring from example code I suppose you might have mistyped "strip".

Anyway, you're passing a string ending in a newline to gethostbyname. It will not resolve.

    print(domains + "\n")
For the code above, I receive the following error on run: socket.gaierror: 
[Errno -2] Name or service not known.

As the exception suggests. When debugging issues like these, _always_ put in print() calls ahead of the failing function to ensure that you are passing the values you think you're passing.

Variation 2:
import csv
import socketdomains = []

with open('top500domains.csv', 'r') as f:
    for line in f:
        line = line.strip()
        domains.append(line)

hostbyname_ex = socket.gethostbyname_ex(str(domains))

gethostbyname_ex takes a single hostname string. You are passing a list of strings.

print(hostbyname_ex)
I receive the same error as the first variation.
Please share your advice and let me know what I am doing wrong. The 
top500domains.csv list is attached.

Plenty of mailing list software strips attachments. In future, unless the file is large, just append the contents below your message (with some explaination).

Cheers,
Cameron Simpson <c...@zip.com.au>

Life is uncertain.  Eat dessert first.  - Jim Blandy
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to