Monday, April 21, 2025

Generating e-mail Addresses with Python

list_e = ['michael', 'john', 'samantha']

domain = 'domain.com'

for x in list_e:
    print(x + '@' + domain)

This Python code creates a list of email names, and for each name in the list, it appends a domain name to the name to create a valid email address.

Here is a line-by-line explanation of the code:

list_e = ['michael', 'john', 'samantha']

This line creates a list named list_e that contains three string values, which are the names of the email recipients.

domain = 'domain.com'

This line assigns a string value of 'domain.com' to the variable domain, which will be used to append to the email names to create the full email address.

for x in list_e:
    print(x + '@' + domain)

This line starts a for loop that iterates over each element x in list_e. For each iteration, it concatenates the email name x with the domain name '@' + domain, using the string concatenation operator +. Finally, it prints the email address to the console.

The output of this script will be:

michael@domain.com
john@domain.com
samantha@domain.com

This is the email address list created by concatenating the name strings with the domain string.

String Formating in Python - .format() Example

a = 1
b = 10
c = 100

print('{} {} {}'.format(a, b, c))

This Python code assigns integer values to three variables a, b, and c respectively, and then prints their values in a formatted string to the console.

Here is a line-by-line explanation of the script:

a = 1
b = 10
c = 100

These lines declare three variables a, b, and c and assign them integer values of 1, 10, and 100, respectively.

print('{} {} {}'.format(a, b, c))

This line prints a formatted string to the console. The string contains three replacement fields (i.e., {}) that will be filled in with the values of a, b, and c, respectively. The format() method is used to substitute these values into the string.

The output of this script will be:

1 10 100

This is the formatted string where the values of a, b, and c have been substituted into the appropriate places.

Code Variant:


a = 1
b = 10
c = 100

print('First: {}, Second: {}, Third: {}'.format(a, b, c))

This line prints a formatted string to the console. The string contains three replacement fields (i.e., {}) that will be filled in with the values of a, b, and c, respectively. The format() method is used to substitute these values into the string. Additionally, each replacement field is preceded by a descriptive label separated by a colon, which is a common way to label the values when using string formatting.

The output of this script will be:

First: 1, Second: 10, Third: 100

This is the formatted string where the values of a, b, and c have been substituted into the appropriate places, and the labels for each value have been added.

Reading a File with For Loop - Python

file = open('some-file.txt', 'r')

backup = open('backup.txt', 'w')

for x in file:
    backup.write(x)

file.close()
backup.close()

How to Add Text to a File - Python File Append

file = open('append-test.txt', 'a')

file.write('------------------ \n')
file.write('more \n')
file.write('lines \n')

file.close()

Write Data to a File - Python File Write

file = open('test-file.txt', 'w')

file.write('first line \n')
file.write('second line \n')

file.close()

How to Read File - Python File Open

#Read all lines from file in IDLE

file = open('bulk-domains.txt', 'r')

res = file.read()

print(res)

 This Python script reads all the lines from a file named bulk-domains.txt in the current directory and prints the contents of the file to the console. Here is a line-by-line explanation:

#Read all lines from file in IDLE

This line is a comment that explains the purpose of the script.

file = open('bulk-domains.txt', 'r')

This line opens the file named bulk-domains.txt in read mode ('r') using the open() function and returns a file object. The file object is assigned to the variable file.

res = file.read()

This line reads the entire contents of the file object into a string variable called res using the read() method.

print(res)

This line prints the contents of the res string variable to the console using the print() function.

This Python script reads the contents of a file named bulk-domains.txt into a string variable and then prints the contents of the string to the console.

Script variant

#Read only 3 lines from file

file = open('bulk-domains.txt', 'r')

line_1 = file.readline()
line_2 = file.readline()
line_3 = file.readline()

print(line_1)
print(line_2)
print(line_3)

This Python script reads the first three lines of a file named bulk-domains.txt and prints them to the console. Here is a line-by-line explanation:

file = open('bulk-domains.txt', 'r')

This line opens the file named bulk-domains.txt in read mode ('r') using the open() function and returns a file object. The file object is assigned to the variable file.

line_1 = file.readline()
line_2 = file.readline()
line_3 = file.readline()

These lines read the first three lines of the file object into three separate string variables called line_1, line_2, and line_3, respectively, using the readline() method.

print(line_1)
print(line_2)
print(line_3)

These lines print the contents of the line_1, line_2, and line_3 string variables to the console using the print() function.

In summary, this Python script reads the first three lines of a file named bulk-domains.txt and prints them to the console. If the file has fewer than three lines, the corresponding variables will be empty strings, and they will be printed as empty lines to the console.

We can use simple for loop here


#Read 3 lines from file with for loop

for x in range(3):
    print(file.readline())

This Python code reads the first three lines of a file and prints them to the console. Here is a line-by-line explanation:

for x in range(3):

This line starts a for loop that will iterate three times, using the range() function to generate the sequence of numbers 0, 1, 2.

    print(file.readline())

This line is indented by four spaces, indicating that it is part of the loop. It does the same thing as the previous line: reads a line from the file and prints it to the console. Because it is inside the loop, it will be executed three times, once for each iteration of the loop.

The combination of these two lines within the loop reads the first three lines of the file and prints them to the console. If the file has fewer than three lines, it will only print the lines that are present in the file.

Python FTP Server with Authentication - pyftpdlib Tutorial

Anonymous FTP Server in Python - pyftpdlib Tutorial

How to Open a URL in Python - Webbrowser Module Tutorial

import webbrowser

chrome = "c:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"

webbrowser.get(chrome).open_new("google.com")

This Python code opens Google Chrome web browser and navigates to the Google homepage. Here is a line-by-line explanation:

import webbrowser

This line imports the webbrowser module, which provides a high-level interface for displaying web-based documents to users.

chrome = "c:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"

This line sets the path to the Google Chrome executable on a Windows machine. The %s is a placeholder that will be replaced with the URL of the website that will be opened.

webbrowser.get(chrome).open_new("google.com")

This line opens the Google Chrome browser and navigates to the google.com homepage. webbrowser.get(chrome) returns a Chrome object that represents the browser instance, and open_new("google.com") opens a new tab in the browser and navigates to the specified URL.

This Python code uses the webbrowser module to open Google Chrome and navigate to the Google homepage on a Windows machine. The path to the Chrome executable is specified, and the open_new method is used to open a new tab with the specified URL.

Bulk WHOIS Reports to .TXT and .ZIP Files

Whois Results to Individual .TXT Files for Every Domain

Domain WHOIS Bulk Lookup Tool

Export WHOIS Lookup to .TXT File

Domain Information Lookup - Python Whois Script

Getting Domain Information - Python WHOIS Example

With this simple Python script we can get information about some doman, using socket module:  

import socket

domain_name = input("Enter a domain name: ")

print("\n-- DNS Information --")
print("Domain Name: ", domain_name)
print("IP Address: ", socket.gethostbyname(domain_name))
print("Canonical Hostname: ", socket.getfqdn(domain_name))
print("Name Server IPs: ", socket.gethostbyname_ex(domain_name)[2])

Let's go through each line of this script in detail to understand what it does:

import socket

This line imports the 'socket' module, which provides access to networking functions in Python.

domain_name = input("Enter a domain name: ")

This line prompts the user to enter a domain name and saves it to the variable 'domain_name'.

print("\n-- DNS Information --")
print("Domain Name: ", domain_name)

These lines print a header and the entered domain name to the console for the user's reference.  

print("IP Address: ", socket.gethostbyname(domain_name))

This line uses the 'socket.gethostbyname()' function to retrieve the IP address of the entered domain name. The IP address is then printed to the console.

print("Canonical Hostname: ", socket.getfqdn(domain_name)) 

This line uses the 'socket.getfqdn()' function to retrieve the canonical hostname of the entered domain name. The canonical hostname is then printed to the console.

print("Canonical Hostname: ", socket.getfqdn(domain_name)) 

This line uses the 'socket.gethostbyname_ex()' function to retrieve a list of IP addresses for the name servers associated with the entered domain name. The list of IP addresses is extracted from the returned tuple using index 2, and is then printed to the console.

This Python script prompts the user for a domain name, retrieves DNS information for the entered domain name using the 'socket' module, and prints the results to the console. It retrieves the IP address, canonical hostname, and name server IPs for the domain name entered by the user.

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 .  ...