C

Python › Module 2 › Lesson 3

BeginnerModule 2Lesson 3/5

Port Scanner Script

Walk through a beginner port scanner design: loops, sockets, and ethical limits

15 min+53 XP3 quiz
Module progress3 of 5
PCSwitchRouterServer
Port loop · Connect probe

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/filtered

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

1

A simple TCP connect scanner detects:

Multiple choice

Knowledge Check

2

True or False: Scanning random internet hosts without permission is acceptable for class homework.

True or False

Knowledge Check

3

A responsible beginner port list is:

Multiple choice

← Previous

Answer all 3 knowledge checks to continue. (0/3 answered)