mkdir ~/tcm/peh/python
nano first.py
# or with Sublime, VsCode installed
subl first.py
code first.py
Example of Python script
#!/bin/python3 - first line
Use # for comments
#!/bin/python3
# Print string <--- this is a comment
print("Hello, world!")
# eventually make the script runnable
chmod +x first.py
# it must containg "#!/bin/python3" as first line, to run it with
./first.py
# Or run it with python3
python3 first.py
#!/bin/python3
import math # Module with various math functions and constants
# Operators
print(10 + 10) # Addition
print(10 - 10) # Subtraction
print(10 * 10) # Multiplication
print(25 / 10) # Division with remainder - float
print(25 // 10) # No remainder division - returns the quotient as an integer
print(25 % 10) # Modulo - returns the remainder of division
print(10 ** 5) # Exponentiation
# Using math functions
print(math.sqrt(500)) # Square root
print(math.pow(2, 10)) # Raise a to power of b
print(math.sin(math.pi/2)) # Calculate sine of pi/2 (in radians)
➡️ A variable is a dynamic container for storing data values.
➡️ A method (or function) is a block of code which runs when called and performs specific operations. It can have return values, perform actions, accept parameters, etc.
nano vars.py
#!/bin/python3
# Variables assignment with different data types
age = 22 # integer
height = 69.5 # float
name = "Sys" # string
is_true = True # boolean
# Variable usage
print(int(age))
print(int(22.7))
print("Hello, " + name + ". Is your height " + str(height) + " cm?")
age += 1 # Add 1 to age variable
print(age)
if is_true:
print("The condition is true \n") # is_true boolean is "True"
# Built-in method
quote = "The quieter you become, the more you can hear."
print(quote.upper()) # upper-case method
print(quote.lower()) # lower-case method
print(quote.title()) # title-case method
length = len(quote) # Count characters
print("Quote length:", length)
print('\n')
➡️ A function is a reusable block of code that performs a specific task.
defined with the def keyword
def function_name():
accepts parameters
❗ Indentation is very important in Python, since it's used to indicate a block of code.
nano func.py
#!/bin/python3
# Function with parameters
def greet(name):
print("Hello, " + name + "!")
# Function call with an argument that is assigned to the "name" param
greet("Sys")
age = 22
# Function without parameters
def who_am_i():
name = "Sys" # Local variable
age = 55 # Local variable
print("My name is " + name + " and I am " + str(age) + " years old.\n")
who_am_i()
print(age) # Not the function's "age" variable
# Parameters
print('Add 100:')
def add_one_hundred(num):
print(num + 100)
add_one_hundred(400) # Call function with an argument
print('Add 2 numbers:')
def add(x,y):
print(x + y)
add(400,300)
print('Multiply 2 numbers:')
# Return statement
def multiply(x,y):
return x * y
multiply(5,5) # Can be assigned to a variable or called
print(multiply(5,5))
result = multiply(9,9)
print(result)
print('Square root:')
def square_root(x):
print(x ** .5)
square_root(64)
print('This is a new line:')
# New line function
def nl():
print('\n')
nl()
➡️ Conditional statements allow the execution of different actions based on certain conditions. Logical math conditions can be used in if statements and loops.
nano conditions.py
#!/bin/python3
# Conditional Statements
def icecream(money):
if money >= 3:
return "Enjoy yourself an icecream!"
else:
return "No icecream for you!"
print(icecream(5)) # 5$ > 3 - if statement is true
print(icecream(1)) # 1$ < 3 - if statement is false
print('\n')
# Multiple conditions
def alcohol(age, money): # Function with multiple parameters
if (age >= 18) and (money >= 5): # Conditional statement, relational operators (boolean)
return "You're getting a Pina Colada!!!"
elif (age >= 18) and (money < 5):
return "You need more money for a drink."
elif (age < 18) and (money >= 5):
return "You are too young kid!"
else:
return "Hey kid, you don't have money nor the age for an alcoholic drink."
print(" Age+Money ok:")
print(alcohol(18,5))
print(" Age ok, but no Money:")
print(alcohol(18,4))
print(" Money ok, but no Age:")
print(alcohol(16,6))
print(" No Age, No Money:")
print(alcohol(17,3))
print('\n')
print('IF-ELSE')
# "if - else" Statement
x = 5
if x > 0:
print("x is positive") # Executed if x > 0 is TRUE
else:
print("x is not positive") # Executed if x > 0 is FALSE
print('IF-ELIF-ELSE')
# Multiple Conditions "if - elif - else" Statement
x = 0
if x > 0:
print("x is positive") # Executed if x > 0 is TRUE
elif x < 0:
print("x is negative") # Executed if x > 0 is FALSE and x < 0 is TRUE
else:
print("x is zero") # Executed if both conditions are FALSE
➡️ Lists are used to store multiple items in a single variable.
list = ["item1", "item2", "item3"]
items are ordered, changeable and indexed (first item index starts at [0])
nano lists.py
#!/bin/python3
# LISTS
print("LISTS")
fruits = ["apple", "banana", "orange"]
# Mutable Lists
fruits[1] = "grape" # Modifying an element
fruits.append("kiwi") # Adding an element to the end
fruits.remove("apple") # Removing an element
print(fruits)
print('\n')
# List Operations
fruits = ["apple", "banana", "orange"]
fruits2 = ["grape", "kiwi"]
print("Fruits: ",fruits)
print("Fruits2: ",fruits2)
combined = fruits + fruits2 # Concatenation
print("Combined: ",combined) # Output: ["apple", "banana", "orange", "grape", "kiwi"]
# Length
print("Fruits list length is:",len(fruits)) # Output: 3 (elements in a list)
# Slicing
sublist = fruits[1:3]
print("Sublist: ",sublist) # Output: ["banana", "orange"]
# List Iteration
print('\n')
print("Fruits list content by iteration:")
for fruit in fruits:
print(fruit) # Output: "apple", "banana", "orange"
# MOVIES
print('\n')
print("MOVIES")
movies = ["Kickboxer", "The Grandmaster", "Ip Man", "Rocky", "Ong Bak", "Rurouni Kenshin Origins"]
print("Movies list is: ",movies)
print(movies[1]) # Returns the second item
print(movies[0]) # Returns the first item
print(movies[1:3]) # Returns 2 items starting from index 1
print(movies[1:]) # Returns all items starting from index 1 to end of list
print(movies[:3]) # Returns everything before index 3
print(movies[-1]) # Returns last item
print("Movies list length is:",len(movies))
movies.append("The Raid 2")
print(movies)
movies.insert(2,"Enter The Dragon")
print(movies)
movies.pop() # Remove the last item
print(movies)
movies.pop(0) # Remove the first item
print(movies)
new_movies = ["Creed III", "John Wick"]
all_movies = movies + new_movies
print("All movies list:",all_movies)
# Two dimentional lists
print('\n')
print("GRADES")
grades = [["Ron", 55], ["Harry", 66], ["Luna", 87]]
rons_grade = grades[0][1]
print(rons_grade)
grades[0][1] = 76
print(grades)
➡️ Looping allows to repeat a block of code multiple times, iterating over a sequence and performing repetitive tasks.
for loop: iterate over a sequence and execute a set of statements for each item in a list, tuple, string, set, dictionary
while loop: execute a set of statements as long as a condition is true
nano loops.py
#!/bin/python3
# Looping
# "for" loops - start to finish of an iterate
print("FOR Loop:")
fruits = ["apple", "banana", "cherry"]
for fruit in fruits: # iterate over each item in "fruits" list
print(fruit)
# "while" loops - execute as long as true, until false
print("\nWHILE Loop:")
count = 0
while count < 10: # as long as true, code will be executed
print(count)
count += 1
# "break" statement - stop the loop prematurely
print("\nBrake:")
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
# "continue" statement - skip current iteration and move to the next one
print("\nContinue:")
fruits = ["apple", "banana", "peach"]
for y in fruits:
if y == "banana":
continue
print(y)
#!/bin/python3
# Advanced Strings
my_name = "sysElement" # this string is immutable
print(my_name[0]) # first letter
print(my_name[-1]) # last letter
sentence = "The quieter you become, the more you can hear."
print("Sentence:\n",sentence)
print(sentence[:3]) # Print everything before the index 3
print("Sentence split:\n",sentence.split()) # Delimiter - default is a "space"
sentence_split = sentence.split()
sentence_join = ' '.join(sentence_split)
print("Sentence join:\n",sentence_join)
# Character escaping with \
quote = 'The master said: "The quieter you become, the more you can hear."'
print(quote)
quote = "The master said: \"The quieter you become, the more you can hear.\""
print(quote)
# Strip
too_much_space = " master "
print(too_much_space.strip(),"\n") # Default delimiter is "space"
# Case insensitive
print("A" in "Apple") # TRUE
print("a" in "Apple") # FALSE
letter = "A"
word = "Apple"
print(letter.lower() in word.lower()) # TRUE
# String Formatting
name = 'Alice'
age = 30
print("My name is %s and I'm %d years old." % (name, age))
# Output: My name is Alice and I'm 30 years old.
movie = "Minions"
print("My favorite movie is {}.".format(movie)) # String format method
print("My favorite movie is %s." % movie) # % formatting
print(f"My favorite movie is {movie}.") # string literal
➡️ Modules are like code libraries, containing a set of functions to include in the application.
reside in external Python files/libraries
import <module>
nano modules.py
#!/bin/python3
# Modules Importing
#import math # Import module
#result = math.sqrt(25)
#print(result)
from math import sqrt # Import specific functions or variables
result = sqrt(25) # Use function without prefixing module name
print(result)
import math as m # Import module as an alias
result = m.sqrt(25)
print(result)
from math import * # Import all functions and variables
result = sqrt(25) # Use functions without prefixing module name
print(result)
import sys # System functions and parameters
from datetime import datetime as dt # Import with alias
print(sys.version)
print(dt.now())
➡️ Sockets are used to send messages across a network, enabling programs to establish connections, send and receive data over various network protocols.
Primary socket module methods and functions are:
socket()
.bind()
.listen()
.accept()
.connect()
.connect_ex()
.send()
.recv()
.close()
nano s.py
#!/bin/python3
import socket
### e.g. 1 - TCP SERVER
# Create a TCP socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# AF_INET is for IPv4 - Address family
# SOCK_STREAM is for the TCP port - Socket type
# Create a UDP socket
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# SOCK_DGRAM is for UDP port
# Bind the socket to a specific address and port
server_address = ('localhost', 1234) # localhost = 127.0.0.1
tcp_socket.bind(server_address)
# Listen for incoming connections
tcp_socket.listen(5) # 5 is the "backlog" parameter
# "backlog" = number of unaccepted connections that the system will allow before refusing new connections
while True:
# Accept a client connection
client_socket, client_address = tcp_socket.accept() # Return value is a pair (conn, address)
# Receive and send data
data = client_socket.recv(1024) # Read data sent by client - 1024 = max buffer size
client_socket.send(b"Received: " + data) # Return the number of bytes sent
# Close the client socket
client_socket.close()
nano s2.py
#!/bin/python3
import socket
### e.g. 2 - Connect to a port
HOST = '127.0.0.1' # localhost loopback address
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST,PORT))
Listen to the 5555 port with nc and launch the s2.py Python script.
the socket will connect to the listening port
nc -nvlp 5555
e.g. Port scanner
Port scanning of an IP address
This port scanner is a proof-of-concept not optimized script, that checks for open ports on a specified target IP within a given range.
nano scanner.py
#!/bin/python3
import sys
import socket
from datetime import datetime
# Define target
if len(sys.argv) == 2:
target = socket.gethostbyname(sys.argv[1]) #Translate hostname to IPv4
else:
print("Invalid amount of arguments.")
print("Syntax: python3 scanner.py <ip>")
#Add a pretty banner
print("-" * 50)
print("Scanning target "+target)
print("Time started: "+str(datetime.now()))
print("-" * 50)
try:
for port in range(50,85):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = s.connect_ex((target,port)) #returns an error indicator - if port is open it throws a 0, otherwise 1
if result == 0:
print("Port {} is open".format(port))
s.close()
except KeyboardInterrupt:
print("\nExiting program.")
sys.exit()
except socket.gaierror:
print("Hostname could not be resolved.")
sys.exit()
except socket.error:
print("Could not connect to server.")
sys.exit()
(ChatGPT) Enhanced script
#!/bin/python3
import sys
import socket
from datetime import datetime
def print_banner(target):
# Prints a banner with the target information and current time.
print("-" * 50)
print(f"Scanning target {target}")
print(f"Time started: {datetime.now()}")
print("-" * 50)
def validate_arguments(args):
# Validates the number of arguments and returns the target address.
if len(args) != 2:
print("Invalid number of arguments.")
print("Syntax: python3 scanner.py <ip>")
sys.exit(1)
return args[1]
def scan_ports(target, start_port=50, end_port=85):
# Scans the ports in the given range on the target IP address.
try:
for port in range(start_port, end_port + 1):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
socket.setdefaulttimeout(1)
result = s.connect_ex((target, port)) # Returns 0 if port is open, otherwise 1
if result == 0:
print(f"Port {port} is open")
except KeyboardInterrupt:
# Handles user interrupt (Ctrl+C)
print("\nExiting program.")
sys.exit(0)
except socket.gaierror:
# Handles errors related to resolving the hostname
print("Hostname could not be resolved.")
sys.exit(1)
except socket.error:
# Handles general socket errors
print("Could not connect to server.")
sys.exit(1)
def main():
# Main function to drive the script.
target = validate_arguments(sys.argv)
target_ip = socket.gethostbyname(target) # Translate hostname to IPv4
print_banner(target_ip)
scan_ports(target_ip)
if __name__ == "__main__":
main()
Try to scan an internal LAN IP
python3 scanner.py 192.168.31.2
# Response
--------------------------------------------------
Scanning target: 192.168.31.2
Time started: 2024-06-29 15:19:30.407598
--------------------------------------------------
Port 53 is open
➡️ input() function is used to take input from the user via the console and return it as a string (by default). Proper validation and error handling is necessary for specific data types and invalid inputs.
nano input.py
#!/bin/python3
# Input
name = input("Enter your name: ") # Prompt
print("Hello, " + name + "!") # Print using the entered name
age = input("Enter your age: ")
age = int(age) # Convert the input string to an integer
print("You will be " + str(age+1) + " next year.")
# Calculator
x = float(input("Give me a number: "))
o = input("Give me an operator: ")
y = float(input("Give me yet another number: "))
if o == "+":
print(x + y)
elif o == "-":
print(x - y)
elif o == "/":
print(x / y)
elif o == "*":
print(x * y)
elif o == "**" or 0 == "^":
print(x ** y)
else:
print("Unknown operator.")
➡️ To read from a file, open it in read mode using open("filename", "r"). Then, use read(), readline(), or readlines() to access its contents.
➡️ To write to a file, open it in write mode using open("filename", "w"). Then, use the write() method to add content to the file.
➡️ To append content to an existing file, open it in append mode using open("filename", "a"). Then, use the write() method to add content.
nano readwrite.py
#!/bin/python3
# READ
# Open the file in read mode
months = open('months.txt', "r")
# Read the entire content
content = months.read()
print(content)
# Read a single line
line = months.readline()
print(line)
# Read all lines
lines = months.readlines()
print(lines)
# Close the file
months.close()
# Alternative way to properly close the file even if exception occurs
with open('months.txt', "r") as months:
content = months.read()
print(content)
# WRITE
# Open the file in write mode
days = open("days.txt", "w")
# Write content to the file
days.write("Hello, World!\n")
days.write("This is a new line.")
# Close the file
days.close()
# APPEND
# Open the file in append mode
days = open("days.txt", "a")
# Append content to the file
days.write("\nMonday")
# Close the file
days.close()
➡️ A class is a blueprint for creating objects, defining their attributes and methods. It serves as a template for creating instances with similar characteristics and behaviors.
➡️ An object is an instance of a class, created using the class as a blueprint. Each object has its own attributes and can use the class's methods. You create objects by calling the class like a function.
nano Employees.py
class Employees:
# This method initializes the attributes of the class
def __init__(self, name, department, role, salary, years_employed):
self.name = name
self.department = department
self.role = role
self.salary = salary
self.years_employed = years_employed
# This method checks if the employee is eligible for retirement
def eligible_for_retirement(self):
if self.years_employed >= 20:
return True
else:
return False
nano ouremployees.py
#!/bin/python3
from Employees import Employees # Import the Employees class from the Employees module
# Create two instances of the Employees class
e1 = Employees("Bob", "Sales", "Director of Sales", 100000, 20)
e2 = Employees("Linda", "Executive", "CIO", 150000, 10)
# Print the name of the first employee
print(e1.name)
# Print the role of the second employee
print(e2.role)
# Check if the first employee is eligible for retirement and print the result
print(e1.eligible_for_retirement())