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

    Start here

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

    Generating Random Data With Python

    Tutorial

    Generating Random Data in Python (Guide)

    Compare the Python random module with its alternatives for generating random data, on security, versatility, purpose, and speed.

    intermediate data-science python

  2. 2

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

    Preventing SQL Injection Attacks With Python

    Tutorial

    Preventing SQL Injection Attacks With Python

    SQL injection attacks are one of the most common web application security risks. In this step-by-step tutorial, you'll learn how you can prevent Python SQL injection. You'll learn how to compose SQL queries with parameters, as well as how …

    intermediate best-practices databases

  3. 3

    Includes coding exercises

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

    Exploring HTTPS With Python

    Course

    Exploring HTTPS and Cryptography in Python

    In this course, you'll gain a working knowledge of the various factors that combine to keep communications over the Internet safe. You'll see concrete examples of how to keep information secure and use cryptography to build your own Python…

    intermediate web-dev

  4. 4

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

    Learning Path

    Network Programming and Security

    5 Resources ⋅ Skills: CRUD Operations, REST APIs, HTTPS, Socket Programming, Cryptography, Network Security, Web Services

  5. +

    Take it anytime

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

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

    Tutorial

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

    Ready to take your Django app beyond development? Learn how to securely deploy your Django web app in production over HTTPS with Gunicorn and Nginx. Along the way, you'll explore how HTTP headers can fortify your app's security.

    intermediate django

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.

Tutorials

In-depth guides to HTTPS, cryptography, sockets, secure randomness, and injection attacks.

Start with HTTPS →

Video courses

Watch an instructor build and break real code, one bite-sized lesson at a time.

Programming Sockets in Python →

Quizzes

Check what you’ve learned in a few minutes and see exactly which concepts to review.

Test your network security skills →

Coding exercises

Write ciphers, key handling, and secure connections in your browser and get instant test feedback.

How exercises work →

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.

secrets
Secure tokens
hashlib
Hashing
hmac
Message signing
getpass
Password prompts
cryptography
Encryption
socket
Network I/O
ipaddress
IPs & networks
Requests
HTTP clients
subprocess
Safe shell calls
logging
Audit trails
Bitwise ops
Bytes & bits
Ruff
Catch risky code

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

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.

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.

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.

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.

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.

Get your free learning plan