Python › Module 2 › Lesson 3
Port Scanner Script
Walk through a beginner port scanner design: loops, sockets, and ethical limits
Opening
Design before you spray ports
A teaching scanner has three parts: target selection, a port list, and a connect test. Production scanners add concurrency, service detection, and strict rate limits—but the idea starts here. Unauthorized scanning can look like an attack. Keep targets in your lab.
1. Scanner Skeleton
Conceptual flowfor each port in list: try connect(host, port) with timeout if success: mark OPEN else: mark closed/filtered
for each port in list:
try connect(host, port) with timeout
if success: mark OPEN
else: mark closed/filtered2. Responsible Defaults
Localhost first
127.0.0.1 while learning.
Small port lists
22, 80, 443—not 1–65535 on day one.
Timeouts
1 second is plenty for lab connect checks.
Logging
Print what you scan so you notice mistakes.
Closed ≠ safe
A closed port just means your connect failed from here. Firewalls can filter. Scanners give clues, not absolute truth.
Knowledge Check
A simple TCP connect scanner detects:
Multiple choice
Knowledge Check
True or False: Scanning random internet hosts without permission is acceptable for class homework.
True or False
Knowledge Check
A responsible beginner port list is:
Multiple choice