You don't need the quotes in the file ('debian.org')
'' just represents a string in python. Remove them from the One_ip file.
To compare, when you typed it into the console, it got 'debian.org', however from the file it read it as ''debian.org'', a string that that quotes around it.
It's also probably getting confused since there might be some newline there
This fixes it -> .strip()
Code:
import socket
def socket_return(input_file):
with open(input_file) as One_ip:
for line in One_ip:
print(line)
ip_addr = socket.gethostbyname(line.strip())
print(ip_addr)
print(socket_return(my_file))
More functional way of doing it:
Code:
#!/usr/bin/env python3
import socket
my_file = 'One_ip'
def socket_return(input_file):
url_ip = list()
with open(input_file) as One_ip:
for line in One_ip:
ip_addr = socket.gethostbyname(line.strip())
url_ip.append((line.strip(), ip_addr))
return url_ip
for url, ip in socket_return(my_file):
print(url, ip)
Or to show results as soon as they show up
Code:
#!/usr/bin/env python3
import socket
my_file = 'One_ip'
def socket_return(input_file):
with open(input_file) as One_ip:
for line in One_ip:
print(line)
ip_addr = socket.gethostbyname(line.strip())
yield ip_addr
for value in socket_return(my_file):
print(value)