Building a Mini Port Scanner in Python (For Beginners)
Have you ever wondered how tools like Nmap scan open ports on a system? In this post, Iβll show you how to create a simple port scanner using Python β great for beginners learning about network security and ethical hacking.
### What Is a Port Scanner?
A
port scanner checks a target machine to see which network ports are open and accepting connections. This is useful in:
- Security audits
- Network troubleshooting
- Ethical hacking and pentesting
Tools We'll Use
Just Pythonβs built-in libraries! No need for external packages.
- socket β for creating TCP connections
- time β for measuring scan duration
Python Code: Mini Port Scanner
```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
import socket
import time
# Get user input
target = input("Enter target IP address: ")
# List of common ports to scan
ports = [21, 22, 23, 25, 53, 80, 110, 139, 143, 443, 445, 3306, 8080]
print(f"\n[+] Scanning {target} on {len(ports)} ports...\n")
start_time = time.time()
for port in ports:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) # 1 second timeout
result = sock.connect_ex((target, port))
if result == 0:
print(f"
Port {port} is OPEN")
else:
print(f"
Port {port} is CLOSED")
sock.close()
end_time = time.time()
print(f"\nScan completed in {round(end_time - start_time, 2)} seconds.")
````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
How It Works
- The socket.connect_ex() method tries to open a TCP connection.
- If it returns 0, the port is open.
- We use a short timeout so it doesn't hang on closed ports.
Want to Take It Further?
- Add multithreading to speed up scans.
- Support full port range (1β65535).
- Detect service banners on open ports.
- Save results to a file.
Contact Me for Free Tool!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<<< AL-Drone13 >>>
<<<
aldrone13@proton.me >>>