A framework for mock FTP servers that relay uploaded files
Project description
ftprelay
ftprelay
is a lightweight Python module that provides a simple framework for setting up a minimal, non-persisting FTP server whose single purpose is to execute custom code on uploaded files before discarding them.
It was developed with the goal of retrofitting older devices or software that exclusively support FTP upload for file transfer, enabling a broader range of applications.
Quick Navigation
Installation
Install ftprelay
using pip:
pip install ftprelay
Usage
- Implement the
process_file()
method in a custom class that inherits fromFileProcessor
. This method defines how uploaded files should be processed. - Implement the
authenticate()
method in a custom class that inherits fromAuthenticator
. This method should either raise anAuthenticationFailedError
or, upon successful authentication, return an instance of the customFileProcessor
that dictates how the file should be processed for this user. - Instantiate and start FTPRelay:
relay = FTPRelay(authenticator=MyCustomAuthenticator(), host='127.0.0.1', port=21)
relay.start()
This initializes an FTP server, where logins are authenticated using your custom Authenticator. Upon successful authentication, uploaded files are temporarily stored. The storage path of each file is then passed to the associated FileProcessor class returned by the Authenticator. Finally, the files are promptly deleted after processing.
Caveats
- Any other operation other than file upload is denied by the FTP server with a 550 error code.
- There are no subfolders on the FTP server, nor does it allow the creation of any. Thus, all files must be directly uploaded to the root directory.
- Using interactive FTP browsers to access the server may result in errors, as they are restricted from reading the contents of the root directory.
Example
A basic example for a FTP relay that sends the uploaded files via email to a recipient address depending on the user.
from dataclasses import dataclass
from pathlib import Path
from ftprelay import AuthenticationFailedError, Authenticator, FileProcessor, FTPRelay
@dataclass
class CustomFileProcessor(FileProcessor):
recipient_email: str
def process_file(self, path: Path) -> None:
# Placeholder code: send email with attachment
send_email(to=self.recipient_email, attachment=path)
class CustomAuthenticator(Authenticator):
def authenticate(self, username: str, password: str) -> FileProcessor:
# Placeholder code: verify credentials
if verify(username, password):
return CustomFileProcessor(recipient_email=f"{username}@example.org")
else:
raise AuthenticationFailedError()
# Instantiate and start the FTPRelay
relay = FTPRelay(authenticator=CustomAuthenticator(), host='127.0.0.1', port=21)
relay.start()
License
ftprelay
is distributed under the terms of the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ftprelay-0.1.tar.gz
.
File metadata
- Download URL: ftprelay-0.1.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.6 Linux/6.5.0-4-amd64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab144979cc627c85bc763bdfaa49df4765b85035546e3b8b49e388cd51686743 |
|
MD5 | 62d6b8bd7d9bf432a092bff4070a26bd |
|
BLAKE2b-256 | e1281ed2a0cbbe4082b588c9bbb5054a6b910a403160426abd6f1b67b669a196 |
File details
Details for the file ftprelay-0.1-py3-none-any.whl
.
File metadata
- Download URL: ftprelay-0.1-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.6 Linux/6.5.0-4-amd64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1943dd29abb613c985fa6c0dc8493723019aa8c3744e43ba1808fd1f1810b8ab |
|
MD5 | f3c77c90fb7cacf2b4932cf9e7b704ec |
|
BLAKE2b-256 | 3588bda3ca5a6c5532df2399d24ba318684178212a5b670f7a6ec98ea13934c4 |