Skill area · Cybersecurity

Write Python that keeps data safe

Secure coding, cryptography, and network fundamentals

Every Python developer handles passwords, tokens, user input, and network traffic. Real Python shows you how to do it safely: generate secrets properly, encrypt data, understand HTTPS and sockets, and avoid the injection bugs attackers look for.

What you’ll be able to do

The security skills every Python developer needs

You don’t need to become a full-time security engineer to write safer code. These are the fundamentals that protect your users, your data, and your servers.

Handle secrets

Generate tokens and passwords that can’t be guessed, hash data, and keep credentials out of your code.

secretshashlibhmac

Encrypt data

Understand symmetric and public-key cryptography, then encrypt and decrypt messages with real libraries.

cryptographyFernetRSA

Block injection

Recognize and fix the classic holes: SQL injection, unsafe eval(), untrusted pickles, and shell commands built from user input.

SQLevalpickle

Understand the network

See how HTTPS protects traffic, work with IP addresses, and build clients and servers with sockets.

HTTPSsocketipaddress
Your roadmap

A clear path to writing secure Python

Not sure where to start? Work through these in order. Each step builds on the one before, and the Network Programming and Security learning path ties it all together with a final quiz.

  1. 1

    Generating Random Data in Python

    Learn why random isn’t safe for passwords and tokens, what “cryptographically secure” means, and how to use the secrets module and hashing instead.

    Tutorial · Start here

  2. 2

    Preventing SQL Injection Attacks With Python

    Exploit a vulnerable query yourself, then fix it with query parameters and safe composition so user input can never rewrite your SQL.

    Tutorial

  3. 3

    Exploring HTTPS and Cryptography in Python

    Sniff unencrypted traffic, then build up from ciphers to public-key cryptography and certificates until you understand how HTTPS keeps data private.

    Video course · Includes coding exercises

  4. 4

    Network Programming and Security

    Put it together: REST APIs, HTTPS, and socket programming, with quizzes along the way and a final knowledge check.

    Learning path · 9 resources

  5. Securely Deploy a Django App With Gunicorn, Nginx, & HTTPS

    Harden a real web app for production: turn on HTTPS, redirect insecure traffic, add a Content Security Policy, and test the result.

    Tutorial · Take it anytime

New to Python itself? Start with Python Basics first.

Try it now

Encrypt a message with a Fernet cipher

This is a real exercise from the Exploring HTTPS and Cryptography in Python video course, using the same library as the example above. Write your functions in the editor and click Run Tests.

Loading exercise...

Running tests, hints, and solutions are included with a Real Python membership. Not a member yet? You can still read the task and write your solution before you join.

Learn it every way

Read it, watch it, test it, practice it

Security concepts stick when you see them break and then fix them yourself. Mix the formats however you like.

Mentor AI

Not sure if your code is safe? Ask right where you are

Security bugs often look like perfectly working code: a token from random, an f-string inside a SQL query, a password committed to a settings file.

Mentor AI sees the tutorial, lesson, or exercise you’re on and your code, and asks the questions that help you spot the problem yourself, so you recognize it next time.

Meet Mentor AI →
The security toolkit

Know the modules that keep your code safe

Much of what you need ships with Python itself. Learn what each tool is for and when to reach for it.

Know the risky corners

Python features that need a security mindset

Some of Python’s most convenient tools are also the easiest to misuse. These tutorials show you where the risks are and how to stay on the safe side.

Questions and answers

Do I need to know Python before I start?

You should be comfortable with Python basics like functions, strings, and working with files. If you’re not there yet, the Python Basics learning path gets you ready.

Is this an ethical hacking or penetration testing course?

No. Real Python focuses on the defensive side: writing secure code, understanding cryptography and HTTPS, and knowing how networks work. These are the foundations you’ll build on whatever direction you take in security.

Do I need a networking or math background?

No. The HTTPS and cryptography material starts from the basics and explains concepts like keys, ciphers, and certificates with working Python code rather than proofs.

Why not just use random for passwords and tokens?

Because its output is predictable to anyone who sees enough of it. Use the secrets module for anything security-related. Generating Random Data in Python explains why.

What’s included in a membership?

Every Real Python membership includes all video courses, quizzes, coding exercises, and learning paths for security and every other topic, along with Mentor AI as it rolls out.

Start writing code you can trust

Get your free learning plan and work through the full security roadmap, with courses, quizzes, coding exercises, and a mentor at your side.