Handler Module#

The handler module contains the FTP protocol implementation and command handling.

The FTP command handler for thinFTP.

This module defines the ThinFTP class which handles FTP commands over a socket connection. It supports standard FTP commands such as USER, PASS, LIST, RETR, STOR, and PASV mode for data transfers.

class thinftp.handler.ThinFTP(request, client_address, server)[source]#

Bases: BaseRequestHandler

The main handler class for the thinFTP server.

This class implements methods for parsing and responding to FTP protocol commands from a connected client. It uses a PASV data connection model.

fileman#

Handles file system operations.

Type:

FileHandler

login_user#

Currently logging-in or logged-in user.

Type:

str

logged_in#

Authentication state of the client.

Type:

bool

transfer_type#

Transfer type (‘A’ for ASCII, ‘I’ for binary).

Type:

str

data_sock#

Passive mode server socket.

Type:

socket.socket

data_conn#

Established data connection with client.

Type:

socket.socket

client_addr()[source]#

Returns the client’s address as a string.

Returns:

The client IP and port in the form ‘host:port’.

Return type:

str

response(sts_code, msg=None, **kwargs)[source]#

Sends an FTP-compliant response message to the client.

Parameters:
  • sts_code (int) – FTP status code.

  • msg (str, optional) – Optional custom message.

  • **kwargs – Format arguments for message templating.

Returns:

The full response line sent to the client.

Return type:

str

handle()[source]#

Entry point for handling a single client connection.

Continuously reads commands, parses them, and dispatches to handler methods. Manages login state and handles QUIT properly.

ftp_user(uname)[source]#

Handle the USER command.

Parameters:

uname (str) – Username from the client.

Returns:

FTP response line.

Return type:

str

ftp_pass(pswd='')[source]#

Handle the PASS command for user login.

Parameters:

pswd (str) – Password from the client.

Returns:

FTP response line.

Return type:

str

ftp_pwd()[source]#

Handle the PWD command to print current working directory.

Returns:

FTP response line.

Return type:

str

ftp_cwd(path)[source]#

Handle the CWD command to change directory.

Parameters:

path (str) – Path to change to.

Returns:

FTP response line.

Return type:

str

ftp_cdup()[source]#

Handle the CDUP command to change to parent directory.

Returns:

FTP response line.

Return type:

str

ftp_mkd(path)[source]#

Handle the MKD command to make a new directory.

Parameters:

path (str) – Path of directory to create.

Returns:

FTP response line.

Return type:

str

ftp_pasv()[source]#

Handle the PASV command to initiate passive data connection.

Returns:

FTP response line.

Return type:

str

ftp_list(path='.')[source]#

Handle the LIST command to list files and directories.

Parameters:

path (str) – Path to list. Defaults to ‘.’.

Returns:

FTP response line.

Return type:

str

ftp_type(arg)[source]#

Handle the TYPE command to set transfer type.

Parameters:

arg (str) – Transfer type (e.g., ‘A’ or ‘I’).

Returns:

FTP response line.

Return type:

str

ftp_retr(fname)[source]#

Handle the RETR command to retrieve a file.

Parameters:

fname (str) – File to download.

Returns:

FTP response line.

Return type:

str

ftp_stor(fname)[source]#

Handle the STOR command to upload a file.

Parameters:

fname (str) – File to store.

Returns:

FTP response line.

Return type:

str

ftp_size(fname)[source]#

Handle the SIZE command to get file size.

Parameters:

fname (str) – File to check.

Returns:

FTP response line.

Return type:

str

ftp_dele(fname)[source]#

Handle the DELE command to delete a file.

Parameters:

fname (str) – File to delete.

Returns:

FTP response line.

Return type:

str

ftp_rmd(path)[source]#

Handle the RMD command to remove a directory.

Parameters:

path (str) – Directory to remove.

Returns:

FTP response line.

Return type:

str

ftp_rnfr(old)[source]#

Handle the RNFR command (rename from).

Parameters:

old (str) – Existing file/directory name.

Returns:

FTP response line.

Return type:

str

ftp_rnto(new)[source]#

Handle the RNTO command (rename to).

Parameters:

new (str) – New file/directory name.

Returns:

FTP response line.

Return type:

str

ftp_feat()[source]#

Handle the FEAT command to list supported features.

Returns:

FTP response line.

Return type:

str

ftp_help(*args)[source]#

Handle the HELP command.

Parameters:

*args – Optional command to get help for.

Returns:

FTP response line.

Return type:

str

ftp_nlst(path='.')[source]#

Handle the NLST command to list names only.

Parameters:

path (str) – Path to list. Defaults to ‘.’.

Returns:

FTP response line.

Return type:

str

ftp_quit()[source]#

Handle the QUIT command to end the session.

Returns:

None

Raises:

ClientQuit – Raised to break from the main loop.

open_data_conn()[source]#

Accepts the incoming data connection from the client.

close_data_conn()[source]#

Closes the current data connection and socket.

ThinFTP Class#

class thinftp.handler.ThinFTP(request, client_address, server)[source]#

Bases: BaseRequestHandler

The main handler class for the thinFTP server.

This class implements methods for parsing and responding to FTP protocol commands from a connected client. It uses a PASV data connection model.

fileman#

Handles file system operations.

Type:

FileHandler

login_user#

Currently logging-in or logged-in user.

Type:

str

logged_in#

Authentication state of the client.

Type:

bool

transfer_type#

Transfer type (‘A’ for ASCII, ‘I’ for binary).

Type:

str

data_sock#

Passive mode server socket.

Type:

socket.socket

data_conn#

Established data connection with client.

Type:

socket.socket

client_addr()[source]#

Returns the client’s address as a string.

Returns:

The client IP and port in the form ‘host:port’.

Return type:

str

response(sts_code, msg=None, **kwargs)[source]#

Sends an FTP-compliant response message to the client.

Parameters:
  • sts_code (int) – FTP status code.

  • msg (str, optional) – Optional custom message.

  • **kwargs – Format arguments for message templating.

Returns:

The full response line sent to the client.

Return type:

str

handle()[source]#

Entry point for handling a single client connection.

Continuously reads commands, parses them, and dispatches to handler methods. Manages login state and handles QUIT properly.

ftp_user(uname)[source]#

Handle the USER command.

Parameters:

uname (str) – Username from the client.

Returns:

FTP response line.

Return type:

str

ftp_pass(pswd='')[source]#

Handle the PASS command for user login.

Parameters:

pswd (str) – Password from the client.

Returns:

FTP response line.

Return type:

str

ftp_pwd()[source]#

Handle the PWD command to print current working directory.

Returns:

FTP response line.

Return type:

str

ftp_cwd(path)[source]#

Handle the CWD command to change directory.

Parameters:

path (str) – Path to change to.

Returns:

FTP response line.

Return type:

str

ftp_cdup()[source]#

Handle the CDUP command to change to parent directory.

Returns:

FTP response line.

Return type:

str

ftp_mkd(path)[source]#

Handle the MKD command to make a new directory.

Parameters:

path (str) – Path of directory to create.

Returns:

FTP response line.

Return type:

str

ftp_pasv()[source]#

Handle the PASV command to initiate passive data connection.

Returns:

FTP response line.

Return type:

str

ftp_list(path='.')[source]#

Handle the LIST command to list files and directories.

Parameters:

path (str) – Path to list. Defaults to ‘.’.

Returns:

FTP response line.

Return type:

str

ftp_type(arg)[source]#

Handle the TYPE command to set transfer type.

Parameters:

arg (str) – Transfer type (e.g., ‘A’ or ‘I’).

Returns:

FTP response line.

Return type:

str

ftp_retr(fname)[source]#

Handle the RETR command to retrieve a file.

Parameters:

fname (str) – File to download.

Returns:

FTP response line.

Return type:

str

ftp_stor(fname)[source]#

Handle the STOR command to upload a file.

Parameters:

fname (str) – File to store.

Returns:

FTP response line.

Return type:

str

ftp_size(fname)[source]#

Handle the SIZE command to get file size.

Parameters:

fname (str) – File to check.

Returns:

FTP response line.

Return type:

str

ftp_dele(fname)[source]#

Handle the DELE command to delete a file.

Parameters:

fname (str) – File to delete.

Returns:

FTP response line.

Return type:

str

ftp_rmd(path)[source]#

Handle the RMD command to remove a directory.

Parameters:

path (str) – Directory to remove.

Returns:

FTP response line.

Return type:

str

ftp_rnfr(old)[source]#

Handle the RNFR command (rename from).

Parameters:

old (str) – Existing file/directory name.

Returns:

FTP response line.

Return type:

str

ftp_rnto(new)[source]#

Handle the RNTO command (rename to).

Parameters:

new (str) – New file/directory name.

Returns:

FTP response line.

Return type:

str

ftp_feat()[source]#

Handle the FEAT command to list supported features.

Returns:

FTP response line.

Return type:

str

ftp_help(*args)[source]#

Handle the HELP command.

Parameters:

*args – Optional command to get help for.

Returns:

FTP response line.

Return type:

str

ftp_nlst(path='.')[source]#

Handle the NLST command to list names only.

Parameters:

path (str) – Path to list. Defaults to ‘.’.

Returns:

FTP response line.

Return type:

str

ftp_quit()[source]#

Handle the QUIT command to end the session.

Returns:

None

Raises:

ClientQuit – Raised to break from the main loop.

open_data_conn()[source]#

Accepts the incoming data connection from the client.

close_data_conn()[source]#

Closes the current data connection and socket.