tftpy

0.8.7 · active · verified Thu Apr 16

Tftpy is a pure Python TFTP implementation that includes both client and server classes. It supports RFCs 1350, 2347, 2348, and the tsize option from RFC 2349, with hooks for progress indicators. The current version is 0.8.7, released on February 4, 2026. The project is actively maintained, with regular updates and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic TFTP server listening on a non-privileged port (6969) and a client downloading a file from it. The server creates a temporary root directory and a sample file. The client then connects to localhost and attempts to download this file. The server runs in a separate thread to allow the client to operate.

import tftpy
import os
import threading
import time

# --- Server Setup ---
# Create a temporary directory for the TFTP server root
server_root_dir = 'tftp_server_root'
os.makedirs(server_root_dir, exist_ok=True)
with open(os.path.join(server_root_dir, 'testfile.txt'), 'w') as f:
    f.write('Hello from TFTP server!')

def start_server():
    print(f"Starting TFTP server on 0.0.0.0:6969 with root {server_root_dir}")
    server = tftpy.TftpServer(server_root_dir)
    # Use a non-privileged port for testing
    try:
        server.listen('0.0.0.0', 6969, timeout=2)
    except Exception as e:
        print(f"Server error: {e}")
    finally:
        print("Server stopped.")
        os.remove(os.path.join(server_root_dir, 'testfile.txt'))
        os.rmdir(server_root_dir)

server_thread = threading.Thread(target=start_server)
server_thread.daemon = True # Allow main program to exit even if server is running
server_thread.start()

# Give the server a moment to start
time.sleep(1)

# --- Client Usage ---
print("\n--- TFTP Client --- ")
client = tftpy.TftpClient('127.0.0.1', 6969)
local_download_path = 'downloaded_testfile.txt'

try:
    print(f"Attempting to download 'testfile.txt' to '{local_download_path}'...")
    client.download('testfile.txt', local_download_path)
    if os.path.exists(local_download_path):
        with open(local_download_path, 'r') as f:
            content = f.read()
        print(f"Download successful! Content: '{content}'")
        os.remove(local_download_path)
    else:
        print("Download failed: file not found locally.")
except tftpy.TftpException as e:
    print(f"TFTP Client Error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

print("Client operations complete.")

view raw JSON →