Monday, April 21, 2025

Domain to IP Converter - Python

import socket, os

list_dom = ['google.com', 'yahoo.com', 'bing.com']

file = open('host-to-ip.txt', 'a')

for x in list_dom:
    #print('IP of domain: ' + x + socket.gethostbyname(x))
    file.write('IP of domain: ' + x + ' -> ' + socket.gethostbyname(x) + '\n')

file.close()

  1. Import the necessary modules socket and os.
  2. Create a list of domains to resolve their IP addresses and store it in the variable list_dom.
  3. Open a new file host-to-ip.txt in append mode and assign it to the variable file.
  4. Iterate over each domain in the list_dom using a for loop: a. Use socket.gethostbyname(x) to resolve the IP address of the current domain x. b. Write the domain name and its IP address to the host-to-ip.txt file.
  5. Close the file.

Explanation of the code: The purpose of this code is to resolve the IP addresses of a list of domains and store them in a file.

The first line of code imports the necessary modules socket and os.

In the next line, a list of domains to resolve their IP addresses is created and stored in the variable list_dom.

The next line opens a new file host-to-ip.txt in append mode and assigns it to the variable file.

The for loop iterates over each domain in the list_dom using the variable x. Inside the for loop, socket.gethostbyname(x) is used to resolve the IP address of the current domain x. The resolved IP address is then written to the file host-to-ip.txt using the write() method of the file object. The string written to the file includes the domain name and its IP address separated by '->'.

Finally, the file is closed using the close() method of the file object. 

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...