Automation & scripting
CLI tools, scheduled jobs, filesystem tasks, and glue scripts that save time.
How to Send an Email with smtplib and a Mock Server in Python
Send an email using smtplib and verify it with a local aiosmtpd mock SMTP server — perfect for testing without a real mail server.
import smtplib
from email.message import EmailMessage
import aiosmtpd.controller as controller
import threading
def handle_message(server, session, envelope):
print(f"Mock server received message:")
print(f"From: {envelope.mail_from}")
print(f"To: {envelope.rcpt_tos}")
print(f"Subject: {envelope.cont…
Mount ISO Loop Device Mock Script in Python
Simulate ISO mounting with a loop device using a mock class — useful for testing scripts that depend on mount/unmount without actual system privileges.
import os
import tempfile
from dataclasses import dataclass
from pathlib import Path
@dataclass
class LoopDevice:
path: str
iso_path: str
mounted: bool = False
def mount(self, mount_point: str):
if self.mounted:
raise RuntimeError(f"Loop device {self.path} already mounted")
…
Browse by section
Each section groups closely related Python snippets.
Automation & scripting — Python code examples
What you will find here
This page collects automation & scripting snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.