Your first Python script, explained

Part 1 of Python for security, a series for security and cloud people who are new to code. Over nine short lessons we build a script that warns you before a website's TLS certificate expires. It starts here, with one file.

  1. What it is

    A text file that Python runs

    A Python script is a plain text file of instructions, named with .py by convention. The file does nothing by itself: the Python interpreter reads it and carries out what it says.

    Source: Python Tutorial, section 2.1

  2. How it works

    Run it with one command

    Open a terminal and type python followed by the file name, for example python certcheck.py. On Windows, py does the same job. Our file holds one instruction so far: print(), which writes a line of text to standard output, your terminal.

    Source: Python docs, print()

  3. Why it matters

    Every certificate has an end date

    A certificate is valid only between two dates, notBefore and notAfter. Software that validates certificates the RFC 5280 way requires the current time to fall inside that window, so once notAfter passes, the certificate is rejected and the connection fails. The script this series builds spots those dates in advance.

    Source: RFC 5280, sections 4.1.2.5 and 6.1.3

The takeaway: a script is a text file of instructions, and the python command runs it. Over the next eight parts, this one file learns to check certificates before they expire.

More short lessons

ADIR also offers a free external pen test for cloud teams: see the offer.

Transcript

Let's build a Python tool that warns you before a website's certificate expires. This is part one. A Python script is a plain text file of instructions, and its name ends in .py. On its own, the file does nothing. The Python interpreter is the program that reads the file and runs the instructions inside it. To run it, open a terminal and type python, a space, and the file name. On Windows, py does the same job. Our file holds one instruction so far: print, which writes a line of text to the terminal. Why build this? Every certificate is valid only between two dates. Once the end date passes, software that checks certificates rejects it, and the connection fails. Our script will spot those dates first, while there's still time to renew. So, a script is a text file of instructions. The python command runs it. And over the next eight parts, ours learns to check certificates before they expire.

Narrated with an AI voice.

Next
Next

Pen test vs vulnerability scan, explained