A beginner-friendly Python port scanner project built to help me learn how TCP port scanning works using Python sockets.
This repository includes:
- a working single-threaded port scanner
- a threaded port scanner currently in progress
- Overview
- Project Goals
- Current Features
- Project Structure
- How the Scanner Works
- Requirements
- Installation
- Usage
- Example Run
- What I Learned
- Planned Improvements
- Disclaimer
This project was created as part of my Python and cybersecurity learning journey.
The purpose of this repository is to practice:
- working with Python modules
- understanding socket programming
- learning how TCP connections are used for port scanning
- validating user input
- measuring scan runtime
- exploring multithreading for performance improvements
The main goals of this project are to:
- understand how the
socketmodule works - build a basic TCP port scanner from scratch
- learn the logic behind checking open ports
- improve code organization with functions
- experiment with threading to speed up scanning
- document progress through GitHub
- accepts a hostname or IP address
- resolves hostnames to an IPv4 address
- validates IP addresses
- scans through a user-defined number of ports
- checks whether a port is open using TCP socket connections
- displays open ports found during the scan
- shows how long the scan took to complete
- uses colored terminal output with
colorama
- intended to scan ports faster using threads
- uses threading-related components such as workers, locks, and queues
- currently still being developed and debugged
python-port-scan/
│
├── port_scan.py
├── threaded_port_scan.py
└── README.md
The first working version of the scanner.
This script focuses on simplicity and understanding the core logic of port scanning.
A work-in-progress version that attempts to improve scan speed using multithreading.
The basic scanner follows this process:
- Ask the user for a target host
- Resolve the hostname to an IP address
- Ask how many ports to scan
- Loop through each port in the chosen range
- Create a TCP socket for each port
- Attempt a connection to the target
- Identify whether the port is open
- Close the socket after the attempt
- Display the total runtime after the scan finishes
- Python 3.x
colorama
Clone the repository:
git clone https://github.com/tbaaaa/python-port-scan.git
cd python-port-scanInstall the required dependency:
pip install coloramaRun the basic scanner with:
python port_scan.pyYou will be prompted to enter:
- the target hostname or IP address
- the number of ports to scan through
Enter the host IP address or domain name to scan: scanme.nmap.org
Enter how many ports to increment through (large number ==> scan takes more time..): 100
Example output:
[+] Port 22 is open!
[+] Port 80 is open!
[+++] Completed port scan!
[+++] 2 ports are open on scanme.nmap.org.
[+++] Scan completed in 4.21 seconds.
Through this project, I practiced and improved my understanding of:
- Python modules and imports
- the
socketmodule - TCP connection logic
- validating IP addresses
- handling user input
- using functions to organize code
- measuring execution time
- using threading concepts like locks and queues
- finish and debug the threaded scanner
- allow users to define custom start and end port ranges
- improve command-line argument handling
- improve error handling and user feedback
- clean up and refactor code
- add comments and documentation throughout the scripts
- possibly add service detection or banner grabbing later
This project is intended for educational purposes and authorized testing only.
Only scan systems that you own or have explicit permission to test.
port_scan.py→ workingthreaded_port_scan.py→ in progress