Overview
Want to master computer networking through working code? This by-example guide teaches essential networking concepts through 83 annotated, runnable Python examples organized by complexity level.
What Is By-Example Learning?
By-example learning is an example-first approach where you learn through annotated, runnable code rather than narrative explanations. Each example is self-contained, immediately executable with python3, and heavily commented to show:
- What each line does — Inline comments explain purpose and mechanism
- Expected outputs — Using
# =>notation to show results - Intermediate values — Variable states and control flow made visible
- Key takeaways — 1-2 sentence summaries of core concepts
This approach suits experienced developers who understand at least one programming language and want to learn networking concepts through working code, not through lengthy prose.
Learning Path
The networking by-example tutorial guides you through 83 examples organized into three progressive levels, from fundamental concepts to advanced patterns.
%% Color Palette: Blue #0173B2, Orange #DE8F05, Teal #029E73, Purple #CC78BC, Brown #CA9161
graph TD
A["Beginner (Examples 1-29)<br/>Fundamentals<br/>Packets, DNS, TCP, HTTP"]
B["Intermediate (Examples 30-56)<br/>Production Patterns<br/>TLS, Multiplexing, Caching"]
C["Advanced (Examples 57-83)<br/>Expert Mastery<br/>Raw sockets, asyncio, observability"]
A -->|Master foundations| B
B -->|Advanced patterns| C
style A fill:#0173B2,stroke:#000,color:#fff
style B fill:#DE8F05,stroke:#000,color:#fff
style C fill:#029E73,stroke:#000,color:#fff
Coverage Philosophy
This guide provides comprehensive coverage of computer networking through practical, annotated examples using Python's standard library. All examples run with python3 — no external packages required.
What Is Covered
- Network fundamentals — OSI model, TCP/IP, IP addressing, subnetting, MAC addresses
- Transport layer — TCP, UDP, sockets, handshakes, teardown, congestion control
- Application layer — HTTP/1.1, HTTP/2, HTTP/3 concepts, WebSockets, gRPC overview
- DNS — Resolution lifecycle, record types, DNS over HTTPS
- Security — TLS handshake, certificates, mTLS, DNSSEC, Zero Trust
- Socket programming — TCP/UDP servers and clients, non-blocking sockets, select(), asyncio
- Network infrastructure — NAT, DHCP, BGP basics, CDN, load balancing, reverse proxy
- Observability — tcpdump concepts, network metrics, flow data, rate limiting
- Advanced topics — Raw sockets, eBPF overview, DPDK overview, QUIC, SR-IOV
What Is NOT Covered
- Network administration — Router configuration, OSPF, hardware setup
- Cloud-specific networking — AWS VPC, Azure VNet, GCP networking APIs
- Network programming frameworks — Twisted, Trio (standard library foundation applies to all)
- Physical layer details — Cable specifications, signaling, modulation
- Full protocol implementations — This guide demonstrates concepts, not production-grade stacks
How to Use This Guide
- Sequential or selective — Read examples in order for progressive learning, or jump to specific topics
- Run everything — Copy each example into a file and run with
python3 example.py. Experimentation solidifies understanding. - Modify and explore — Change values, add print statements, break things intentionally
- Use as reference — Bookmark examples for quick lookups
Best workflow: Open your terminal in one window, this guide in another. Run each example as you read it.
Structure of Each Example
Every example follows a consistent five-part format:
- Brief Explanation (2-3 sentences): What the example demonstrates and why it matters
- Mermaid Diagram (optional): Visual clarification when concept relationships benefit from visualization
- Heavily Annotated Code: Every significant line includes a comment explaining what it does and what it produces (using
# =>notation) - Key Takeaway (1-2 sentences): The core insight from this example
- Why It Matters (50-100 words): Production relevance and real-world application
Prerequisites
- Basic Python knowledge (variables, functions, loops, classes)
- Basic understanding that computers communicate over networks
- Python 3.8+ installed
- No external packages needed — all examples use the standard library
Learning Strategies
For Web Developers
You understand HTTP requests and responses. Networking by-example deepens that knowledge:
- Focus on Examples 18-26 (HTTP structure, methods, headers) to see what happens under the hood
- Then move to Examples 38-40 (TLS) to understand HTTPS security
- Examples 35-37 (HTTP/2, HTTP/3, WebSockets) show modern web protocol innovations
For Backend Engineers
You build services that communicate over networks. This guide shows the protocols your services use:
- Start with Examples 10-17 (TCP/UDP sockets) to understand what your frameworks abstract
- Study Examples 30-34 (socket options, multiplexing, threading) to understand server architectures
- Focus on Examples 59-61 (asyncio) to understand async I/O patterns
For System Administrators
You configure and troubleshoot networks. This guide shows the code behind the protocols:
- Examples 42-44 (NAT, DHCP, BGP) show protocols you configure daily
- Examples 72-73 (tcpdump, performance testing) demonstrate the tools you use
- Examples 66-68 (firewalls, eBPF, DPDK) explain advanced infrastructure concepts
For Security Engineers
You protect networked systems. This guide shows the attack surface and defenses:
- Focus on Examples 38-41 (TLS, certificates, ssl module) to understand HTTPS mechanics
- Study Examples 52, 74-77 (port scanning, Zero Trust, mTLS, SOCKS, DNSSEC) for security patterns
- Examples 57-58 (raw sockets, packet crafting) show how packet-level inspection works
Relationship to Other Networking Content
| Tutorial Type | Coverage | Best For |
|---|---|---|
| Introduction | Conceptual | Learning from scratch |
| This: By Example | Comprehensive | Rapid depth for experienced devs |
The Introduction provides narrative context. By-example provides working code. Use both for complete understanding.
Examples by Level
Beginner (Examples 1–29)
- Example 1: What Is a Network — Hosts, Packets, Links
- Example 2: OSI Model Layers
- Example 3: TCP/IP Model vs OSI Model
- Example 4: IP Addresses — IPv4 and CIDR Notation
- Example 5: Subnet Masks and Subnetting
- Example 6: IPv6 Addresses
- Example 7: MAC Addresses and ARP
- Example 8: DNS Resolution — Query Lifecycle
- Example 9: DNS Record Types
- Example 10: UDP Basics — Python Socket
- Example 11: TCP Basics — Python Socket
- Example 12: TCP Three-Way Handshake
- Example 13: TCP Four-Way Teardown
- Example 14: Port Numbers — Well-Known vs Ephemeral
- Example 15: Socket Programming — TCP Server
- Example 16: Socket Programming — TCP Client
- Example 17: Socket Programming — UDP Server and Client
- Example 18: HTTP/1.1 Request Structure
- Example 19: HTTP/1.1 Response Structure
- Example 20: HTTP Methods
- Example 21: HTTP Status Codes
- Example 22: HTTP Headers — Common Ones
- Example 23: URL Parsing — Python urllib.parse
- Example 24: Making HTTP Requests — Python http.client
- Example 25: HTTPS and TLS Basics
- Example 26: Cookies and Sessions Overview
- Example 27: Network Interfaces — Loopback and LAN
- Example 28: Ping and ICMP
- Example 29: Traceroute Concept
Intermediate (Examples 30–56)
- Example 30: TCP Socket Options — SO_REUSEADDR and TCP_NODELAY
- Example 31: Non-Blocking Sockets
- Example 32: select() for I/O Multiplexing
- Example 33: Threading Model — One Thread Per Connection
- Example 34: Python Threading with Sockets
- Example 35: HTTP/2 Concepts — Multiplexing and Frames
- Example 36: HTTP/3 and QUIC Overview
- Example 37: WebSockets — Handshake and Frames
- Example 38: TLS Handshake Deep-Dive
- Example 39: TLS Certificates — Chain of Trust
- Example 40: Python ssl Module — Wrapping Sockets
- Example 41: DNS over HTTPS (DoH) Overview
- Example 42: NAT — Network Address Translation
- Example 43: DHCP — Dynamic Host Configuration
- Example 44: BGP Basics — Autonomous Systems
- Example 45: Load Balancing Strategies
- Example 46: Reverse Proxy Concept
- Example 47: CDN Fundamentals
- Example 48: TCP Congestion Control — Slow Start and AIMD
- Example 49: TCP Flow Control — Window Size
- Example 50: Packet Fragmentation and MTU
- Example 51: ICMP Error Messages
- Example 52: Port Scanning Concepts — Python Socket
- Example 53: HTTP Caching — Cache-Control and ETag
- Example 54: HTTP Authentication — Basic and Bearer Tokens
- Example 55: Multicast and Broadcast
- Example 56: Network Namespaces Overview
Advanced (Examples 57–83)
- Example 57: Raw Sockets — Python
- Example 58: Packet Crafting with struct Module
- Example 59: Python asyncio for Async Networking
- Example 60: Async TCP Server with asyncio
- Example 61: Async HTTP Client with asyncio
- Example 62: gRPC Concepts — Protobuf and Streams
- Example 63: MQTT Protocol — Pub/Sub for IoT
- Example 64: WebRTC Overview — ICE, STUN, TURN
- Example 65: VPN — Tunnel and Encryption Overview
- Example 66: Firewall Rules — iptables Concepts
- Example 67: eBPF Basics for Networking
- Example 68: DPDK and Kernel Bypass Overview
- Example 69: TCP BBR Congestion Control
- Example 70: QUIC Implementation Concepts
- Example 71: Network Observability — Metrics, Flow Data
- Example 72: tcpdump and Wireshark Concepts
- Example 73: Network Performance Testing — Throughput, Latency, Jitter
- Example 74: Zero Trust Networking Model
- Example 75: mTLS — Mutual TLS
- Example 76: SOCKS Proxy Protocol
- Example 77: DNS Security — DNSSEC
- Example 78: Anycast Routing
- Example 79: SR-IOV and Virtualized Networking
- Example 80: IPv6 Migration Strategies
- Example 81: Network Simulation — Python Socket Tricks
- Example 82: Rate Limiting Algorithms — Token Bucket and Leaky Bucket
- Example 83: Production Networking Checklist and Patterns
Last updated May 9, 2026