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
- Thesocket
module is imported to enable socket programming in Python.file = open('bulk-domains.txt', 'r')
- This line of code opens a file namedbulk-domains.txt
in read mode and assigns it to the variablefile
.domain_list = []
- An empty list nameddomain_list
is created to hold the domain names from thebulk-domains.txt
file.for x in file.readlines():
- Thereadlines()
method reads each line of thebulk-domains.txt
file and returns a list of strings. Thefor
loop 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:
- Thefor
loop 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.
No comments:
Post a Comment