import socket
Define the target host and the range of ports to scan
target_host = "example.com"
target_ports = [80, 443, 22, 8080]
Function to perform a port scan
def port_scan(target_host, target_ports):
for port in target_ports:
try:
# Create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Set a timeout for the connection attempt
socket.setdefaulttimeout(2)
# Attempt to connect to the target host and port
client.connect((target_host, port))
# If the connection is successful, print an open port message
print(f"Port {port} is open")
# Close the socket
client.close()
except socket.error:
# If the connection fails, print a closed port message
print(f"Port {port} is closed")
Call the port_scan function
port_scan(target_host, target_ports)
import socket
Define the target host and the range of ports to scan
target_host = "example.com"
target_ports = [80, 443, 22, 8080]
Function to perform a port scan
def port_scan(target_host, target_ports):
for port in target_ports:
try:
# Create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Call the port_scan function
port_scan(target_host, target_ports)