A minimal command‑line port scanner written in pure Python. Give it one or more targets (IPv4 address or hostname), choose how many ports you want to probe, and the script will quickly report which ports are open and, when possible, grab the service banner.
- Scan a single host or multiple hosts (comma‑separated) in one run
- Banner grabbing – fetch up to 1 KB from open sockets to identify running services
- Accepts hostnames or raw IP addresses (uses
IPy
to validate/convert) - Adjustable speed/accuracy – change the socket timeout to trade accuracy for speed
- Zero external dependencies beyond
IPy
# Clone the repo
$ git clone https://github.com/youruser/python-port-scanner.git
$ cd python-port-scanner
# (Optional) create a virtual environment
$ python -m venv .venv && source .venv/bin/activate
# Run the scanner
$ python port_scanner.py
You’ll be prompted for the target(s) and the number of ports to scan.
$ python port_scanner.py
[+] Enter target/s to scan (split multiple targets with ,): scanme.nmap.org
[+] Enter number of ports you want to scan: 1000
$ python port_scanner.py
[+] Enter target/s to scan (split multiple targets with ,): 192.168.1.10,example.com,10.0.0.5
[+] Enter number of ports you want to scan: 500
If you need higher accuracy (at the cost of speed), increase the timeout:
sock.settimeout(1) # default is 0.5 seconds
python-port-scanner/
├── port_scanner.py # main script (≈90 lines)
└── README.md # you are here
- Input parsing – Accept a single string of targets, split on commas, and normalise each to an IPv4 address with
IPy
/socket.gethostbyname()
. - Port loop – For every port in
range(1, n)
create a TCP socket and attempt toconnect()
. - Timeout – Sockets time out after 0.5 s (configurable) to keep the scan quick.
- Banner grabbing – If a connection succeeds, attempt to
recv(1024)
bytes to capture the service banner. - Output – Print
[+] Open port {port}: {banner}
or simply[+] Open port {port}
if no banner is returned.
Because closed/filtered ports raise exceptions, they’re silently ignored to keep the output clean.
- Python 3.7 or newer
IPy
(install viapip
)
Mohamed Ezzat – mohamedaezzat.github.io
I would highly recommend checking out his GitHub for learning material with hands-on projects