Content of bulk-domains.txt
google.com bing.com yahoo.com facebook.com
import socket
file = open('bulk-domains.txt', 'r')
domain_list = []
for x in file.readlines():
domain_list.append(x.rstrip())
for y in domain_list:
print(y + ' -> ' + socket.gethostbyname(y))
Detailed explanation of the code:
import socket- Thesocketmodule is imported to enable socket programming in Python.file = open('bulk-domains.txt', 'r')- This line of code opens a file namedbulk-domains.txtin read mode and assigns it to the variablefile.domain_list = []- An empty list nameddomain_listis created to hold the domain names from thebulk-domains.txtfile.for x in file.readlines():- Thereadlines()method reads each line of thebulk-domains.txtfile and returns a list of strings. Theforloop iterates over each string in the list.domain_list.append(x.rstrip())- Therstrip()method removes the newline character at the end of each string, and the resulting string is appended to thedomain_list.for y in domain_list:- Theforloop iterates over each item in thedomain_list.socket.gethostbyname(y)- Thegethostbyname()method takes a domain name as an argument and returns its corresponding IP address.print(y + ' -> ' + socket.gethostbyname(y))- The domain name and its corresponding IP address are printed to the console with an arrow symbol between them.
This code reads a list of domain names from a file, obtains their IP addresses using the socket module, and prints the domain name and IP address pairs to the console.