
One of the first skills every aspiring ethical hacker should learn is network reconnaissance. Before security professionals can identify vulnerabilities, they must first understand which systems, services, and ports are exposed on a network.
Port scanning is a fundamental technique used during the reconnaissance phase of ethical hacking and penetration testing. It helps security professionals discover open ports and running services on target systems.
In this hands-on tutorial, you will build a simple Python-based Port Scanner from scratch. This project is beginner-friendly and will help you understand networking concepts, socket programming, and ethical hacking fundamentals.
Important Note: Only scan systems that you own or have explicit permission to test.
A port scanner is a tool that checks whether specific network ports on a device are open, closed, or filtered.
Common examples include:
Nmap
Masscan
Angry IP Scanner
By creating your own port scanner, you'll understand how these tools work behind the scenes.
By completing this project, you will learn:
TCP/IP Networking Basics
Socket Programming
Open Port Detection
Python Fundamentals
Ethical Hacking Reconnaissance Techniques
Before starting, make sure you have:
Python 3.x
VS Code or any Python IDE
Internet Connection
Basic Python
Basic Networking Concepts
IP Addresses
Ports
Ports allow applications to communicate over networks.
Examples:
PortService21FTP22SSH25SMTP53DNS80HTTP443HTTPS
When a port is open, a service is actively listening for connections.
User Inputs Target IP ↓
Python Scanner ↓
Checks Port Availability ↓
Reports Open Ports
Create a folder named:
PortScannerProject
Inside it create:
port_scanner.py
Open the Python file and add:
import socket
The socket module allows Python to communicate using network protocols.
Add:
target = input("Enter Target IP Address: ")
Example:
192.168.1.1
Add:
def scan_port(ip, port): try: sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) sock.settimeout(1) result = sock.connect_ex((ip, port)) if result == 0: print(f"Port {port} is OPEN") sock.close() except: pass
Creates TCP socket
Attempts connection
Checks if connection succeeds
Displays open ports
Add:
print(f"Scanning {target}...") for port in range(1, 1025): scan_port(target, port)
This scans ports:
1 → 1024
These are commonly used service ports.
Final code:
import socket target = input("Enter Target IP Address: ") def scan_port(ip, port): try: sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) sock.settimeout(1) result = sock.connect_ex((ip, port)) if result == 0: print(f"Port {port} is OPEN") sock.close() except: pass print(f"Scanning {target}...") for port in range(1, 1025): scan_port(target, port)
Execute:
python port_scanner.py
Example:
Enter Target IP Address:
127.0.0.1
Output:
Scanning 127.0.0.1 Port 80 is OPEN
Port 443 is OPEN
Port 3306 is OPEN
A service is actively listening.
No service is listening.
Firewall may be blocking access.
Ethical hackers use port scanning to:
Discover exposed services
Identify attack surfaces
Map network infrastructure
Verify firewall configurations
System administrators use it to:
Audit security
Monitor services
Troubleshoot networks
After building the basic scanner, add:
Scan faster.
Identify running services.
Example:
Port 80 → HTTP
Port 22 → SSH
Retrieve service information.
Build using Tkinter.
Generate CSV reports.
Professional tools built on similar principles include:
Nmap
Zenmap
Masscan
Nessus
OpenVAS
Understanding your own scanner helps you understand these professional tools.
Add these to your resume:
TCP/IP
Ports
Services
Functions
Loops
Error Handling
Reconnaissance
Port Scanning
Network Enumeration
When publishing this project on GitHub, include:
Purpose of the scanner.
Terminal output.
Open port detection
Fast scanning
Simple interface
Planned improvements.
After completing this mini-project, you will understand:
How networks communicate
How TCP connections work
How ethical hackers discover services
Basic socket programming
Python-based security automation
This project forms an excellent foundation for advanced cybersecurity topics such as vulnerability scanning, network mapping, penetration testing, and ethical hacking.
After this project, consider:
Network Scanner
Banner Grabber
Password Strength Checker
Vulnerability Scanner
Wireshark Packet Analysis Project
Web Application Security Scanner
Each project will progressively strengthen your ethical hacking and cybersecurity skills.
Building your own port scanner is one of the best beginner cybersecurity projects because it combines networking, Python programming, and ethical hacking concepts into a practical, real-world application.
The goal isn't just to create a tool—it is to understand the concepts behind reconnaissance and network security. As you continue your cybersecurity journey, this knowledge will become the foundation for more advanced penetration testing and ethical hacking techniques.

Facebook
Instagram
X
LinkedIn
Youtube
WhatsApp