Network programming with Python: sockets, HTTP, TLS
You write clients and servers with sockets, understand HTTP and TLS from the ground up and build network code that handles timeouts and errors; you only work against your own lab targets.
Level 301 · 20 lessons · 21 practice labs
Lessons
Sockets
- What a socket is: address, port and connection
- A TCP client: connect, send, receive
- A TCP server and multiple clients
- Message boundaries: length prefixes and line endings
- UDP sockets
- Timeouts and network errors
- Short task: write your own small text protocol
Name resolution and HTTP
- getaddrinfo, IPv4 and IPv6
- Writing an HTTP request by hand
- HTTP with http.client and urllib
- A small server with http.server and its limits
- Sessions, retries and connection pooling with httpx
- Short task: fetch every record from a paginated API
TLS and secure communication
- A TLS client with the ssl module
- Certificate verification and why you never turn it off
- A TLS server with your own certificate
- A non-blocking server with selectors
- A server with asyncio streams
- Short task: report certificate expiry dates of the lab servers
Capstone
- Capstone: a multi-user chat server over TLS
Practice labs
- A client for a text protocol with multi-line replies: Write a state-machine client for a mail-like text protocol with multi-line replies that closes the session cleanly on an unexpected code.
- Reading a lab service's greeting line: Connect over TCP to the text service on the lab target, read its first line and close the connection with a with block.
- Reading until the line is complete: Fix a client that trusts one recv call so it accumulates until a newline arrives and fragmented replies are joined correctly in the test.
- A multi-client quote server: Write a threaded server that sends each connection a random quote, where a slow client does not hold up the others.
- A connection checker that tells errors apart: Write a checker for the lab targets that reports refused connections, timeouts and name resolution failures separately.
- Length-prefixed JSON messages: Write two functions that send and receive JSON with a 4-byte length prefix, joining partial reads correctly.
- A heartbeat monitor over UDP: Write a monitor that listens for UDP heartbeats from devices and lists those silent for ten seconds.
- Capping the message size: Add a size cap and timeout to a receiver that trusts the length field and allocates without limit, closing the connection on huge lengths.
- Listing a name's IPv4 and IPv6 addresses: Use getaddrinfo to split the lab host names' addresses by family and print them as a table.
- Reading headers with a hand-written HEAD request: Send a HEAD request over a socket to the lab web target and parse the status line and headers into a dictionary.
- Requests with retries through one client: Restructure code that opens a new connection per request around one httpx.Client with a retrying transport so the connection count drops in the test.
- Following cursor pagination to the end: Collect every record from the lab API's endpoint paged by a next cursor without looping forever on a repeated cursor.
- Reading a server certificate's fields: Connect to the lab TLS target with the ssl module and print the certificate's subject, issuer and expiry date.
- Turning certificate verification back on: Fix a client that disables verification by using a default context trusting the lab CA file so the wrongly named server is refused.
- Closing old versions on a TLS server: Set the minimum version to TLS 1.2 on a TLS server using your own certificate and show the old-version test client is refused.
- A single-threaded server with selectors: Write an upper-casing server with the selectors module that answers several clients at once.
- A key-value server with asyncio streams: Write an asyncio streams server that understands GET and SET line commands and answers unknown commands with an error line.
- Closing idle connections: Add a timeout that closes clients silent for thirty seconds and a connection limit to an async server.
- Reassembling frames that arrive in pieces: Fix a receiver for length-prefixed messages so it also works when one message arrives in several pieces or two messages arrive in one piece.
- Timeouts and retries for a UDP query: Add a timeout and retries with growing waits to a client that queries the lab server over UDP, matching replies by request number and ignoring unrelated packets.
- Making requests with timeouts through one client: Move code that opens a new connection per call onto a single client object, set connect and read timeouts and turn error status codes into exceptions.
About this course
You write clients and servers with sockets, understand HTTP and TLS from the ground up and build network code that handles timeouts and errors; you only work against your own lab targets.
What you will learn
- Sockets
- Name resolution and HTTP
- TLS and secure communication
- Capstone
Prerequisites
You should be comfortable with the level 101 and 201 topics of the same path.
Lab environment
Lessons run in your own Kali Linux terminal in the browser, connected to a private lab network. Nothing to install.
Frequently asked questions
- Is this course right for my level?
- The course is at level 301; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.