I am having a hard time figuring out how to get python to do a DNS lookup for every IP address in my text file. I am getting a TypeError: getaddrinfo() argument 1 must be string or None. Can anyone lend a hand with this?
import socket
ip_list = open('output.txt', 'r')
ais = socket.getaddrinfo(ip_list, 0)
print(ais)
You are passing the file
iteable object directly as argument to socket.getaddrinfo
when you need to iterate over the file iterator and pass the strings to the function instead. Assuming each line contains an IP:
with open('output.txt') as f:
for ip in f:
out = socket.getaddrinfo(ip, 0)
...
Notes:
Use with open...
context manager to close()
the file object after automatically without explicitly needing to call close()
even in case of error
By default open()
opens a file in read (r
) mode, so specifying r
is redundant, would not hurt BTW.