[Tutor] Socket Module

Nym City nymcity at yahoo.com
Sat Jul 18 23:07:59 CEST 2015


Thank you all for your responses. I have a follow up question:

So if gethostbyname_ex() takes only a single hostname string, how can I use it to go through a list of hostnames and get their IP resolution as an output? 

This would mean,socket.gethostbyaddr() would also not work for my second project.
 Thank you. 


     On Monday, July 13, 2015 2:59 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
   
 

 On 12/07/15 23:36, Nym City via Tutor wrote:

> import csv
> import socket
>
> domains = open('top500domains.csv', 'r')
> for domain in domains:
>      domain = socket.gethostbyname(str(domains))

You are passing your file object to gethostbyname()

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

> Variation 2:
> import csv
> import socketdomains = []

I assume the domains bit is on a separate line?

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

Again you are passing the entire list to gethostbyname_ex()


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  


More information about the Tutor mailing list