Python libraries: pip, requests, argparse
pip, virtual environments and the useful parts of the standard library.
Level 201 · 16 lessons · 15 practice labs
Lessons
Environments and packages
- Python: pip and virtual environments: You learn to install libraries with pip and to keep each project's dependencies separate with a virtual environment.
- Auditing dependencies: pip-audit and version pinning: You scan your project’s packages for known vulnerabilities with pip-audit and pin versions in a requirements file.
- Packaging your own project: pyproject.toml: You turn your code into a package with pyproject.toml and ship it as an installable command-line tool.
- Short task: a reproducible environment with pinned versions
The standard library
- The useful parts of the Python standard library: You get to know the built-in modules that help most in everyday work and when to reach for each one.
- File work with Python pathlib and CSV: You learn to handle file paths comfortably with pathlib and to read and write CSV files.
- Dates, times and time zones in Python: You learn to handle dates and times correctly, convert between time zones, and compare timestamps in log entries.
- Context managers: with, contextlib: You manage files, connections and locks with with so they close even on errors, and write your own context manager.
- Regular expressions with the re module: You search text for patterns, pull out parts with groups and replace them, handling log and data extraction in short code.
- Secure randomness and hashing: secrets, hashlib, hmac: You generate unpredictable tokens, hash files and verify that a message was not altered using hmac.
- Short task: list the SHA-256 of every file in a folder
Network, command line and tests
- HTTP requests with Python requests: You learn to send requests to web addresses with the requests library and read the response, status code and errors properly.
- Command-line tools with Python argparse: You learn to add options and help text to your scripts with argparse and turn them into real command-line tools.
- Unit testing in Python: You learn to check small parts of your code with automated tests and see right away whether a change broke something.
- Short task: a command-line tool that asks a service for its status
Capstone
- Capstone: a Python tool that produces a daily report: In a fictional company you write a small tool, with tests, that reads data and produces a tidy report file every day.
Practice labs
- A command-line tool that verifies a checksum manifest: Write a tool that verifies file hashes from a manifest, reporting missing, changed and extra files separately with a distinct exit code for each case.
- Installing dependencies in a virtual environment: Create a fresh virtual environment, install the versions in requirements.txt and confirm the versions the tests expect are installed.
- Pinning and auditing dependencies: Pin the dependencies of a project with loose versions, upgrade the vulnerable package from the pip-audit report to a safe version and pass the tests.
- Defining a command in pyproject.toml: Package a small tool with pyproject.toml and define a console command; the tests check the package installs and the command runs.
- Counting days until a contract renewal: Write a function that uses date objects to count the days to an end date and returns a warning when fewer than 30 remain.
- Converting a meeting time across time zones: Write a function that converts a meeting in Istanbul time into each office's local time with zoneinfo and pass the daylight-saving tests.
- Calculating shift lengths correctly across DST: Fix the shift length calculation that goes wrong across daylight-saving changes in Narçiçeği Hospital's night shifts by using time-zone-aware datetimes.
- Generating a password reset code: Write functions that use the secrets module to produce a URL-safe reset token and a six-digit verification code.
- Hashing a large file in chunks: Write a function that reads a file in chunks inside a with block, computes its SHA-256 and compares it with an expected digest.
- Signing a message with HMAC: Write two functions that sign a message with a shared key and verify the signature with hmac.compare_digest, rejecting altered messages.
- Replacing a predictable code with secure randomness: Move an invite-code module that uses random and a timestamp to secrets, set the alphabet and length by policy and pass the tests.
- Picking fields from an API response: Write a function that fetches data from the product API on the lab web target with requests and returns only the name and price fields.
- Handling timeouts and errors in requests: Write a client function that uses timeouts, raise_for_status and clear error messages when calling slow and failing endpoints.
- Writing an argparse interface for a converter: Write an argparse interface that takes input, output and format options and exits with help text on an invalid choice, passing the unittest tests.
- Writing unittest tests for an HTTP client: Write unittest tests with fake responses covering success, 404 and timeout for a client function that calls a weather service.
About this course
pip, virtual environments and the useful parts of the standard library.
What you will learn
- Environments and packages
- The standard library
- Network, command line and tests
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
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 201; 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.