- Joined
- May 11, 2025
- Messages
- 21
- Reaction score
- 25
- Points
- 3
- Thread Author
- #1

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.
###

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

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"

else:
print(f"

sock.close()
end_time = time.time()
print(f"\nScan completed in {round(end_time - start_time, 2)} seconds.")
````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

- 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.

- Add multithreading to speed up scans.
- Support full port range (1–65535).
- Detect service banners on open ports.
- Save results to a file.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<<< AL-Drone13 >>>
<<< aldrone13@proton.me >>>