Aller au contenu

Simple Python FTP server

If you want to serve a local folder easily, below is a short FTP server based on Python and the pyftpdlib package. This lightweight setup allows you to quickly expose any directory on your machine over the network, making it accessible to other computers via standard FTP clients and anonymous login. By using pyftpdlib, you can create a fully functional server with minimal code, supporting user authentication, read and write permissions, and even passive mode connections. This makes it ideal for quick file sharing, testing environments, or internal development purposes without the need for complex external servers. Additionally, because the server is written in Python, it is highly portable and can run on most operating systems, including Windows, macOS, and Linux. You can extend this basic server to include custom logging, bandwidth limits, or access restrictions if needed. This approach is perfect for developers or hobbyists who want a fast, flexible, and easily configurable way to share files locally or across a small network.

#!/usr/bin/python3
import os
import sys
import socket
import threading
import time
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
# Auto-detect folder containing this script (for Finder double-click)
directory = os.path.dirname(os.path.abspath(__file__))
authorizer = DummyAuthorizer()
# Grant anonymous user full permissions including write access
# Files will be created with the permissions of the user running this script
authorizer.add_anonymous(directory, perm="elradfmwMT")
handler = FTPHandler
handler.authorizer = authorizer
# Data transfer settings for streaming
handler.timeout = 7200 # 2 hour timeout for control commands
handler.dtp_handler.timeout = 7200 # 2 hour timeout for data transfers
# Connection pool settings
handler.max_cons = 100
handler.max_cons_per_ip = 100 # Allow multiple connections per IP
# Enable passive mode for better streaming support
handler.permit_foreign_addresses = True
handler.passive_ports = range(1024, 65535) # All non-privileged ports
server = FTPServer(("0.0.0.0", 2121), handler)
server.max_cons = 100
print(f"FTP server running on port 2121, sharing: {directory}")
print("✓ Optimized for streaming clients with keep-alive support")
print("✓ 2-hour timeouts for long-running transfers")
print("✓ Connection limits: 50 total, 50 per IP")
print("✓ TCP keep-alive enabled (60s idle, 30s probes, 5 attempts)")
print("✓ Socket buffers optimized (256KB send/recv)")
print("✓ TCP_NODELAY enabled for low-latency streaming")
print("Access via ftp://your-ip:2121 (anonymous)")
print("\nServer started…")
server.serve_forever()
view raw ftpserverpy hosted with ❤ by GitHub

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *