cdm_data_loaders.utils.file_transfer.ftp_client ¶
General-purpose FTP client utilities.
Provides resilient FTP connections with TCP keepalive, NOOP pings, retry on transient errors, and thread-local connection management for parallel downloads. Protocol-agnostic — callers supply the FTP hostname.
ThreadLocalFTP ¶
Manage thread-local FTP connections for parallel downloads.
Each thread gets its own FTP connection, created on first access.
Call :meth:close_all when done to cleanly shut down all connections.
Source code in src/cdm_data_loaders/utils/file_transfer/ftp_client.py
__init__ ¶
Initialise with FTP host and timeout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
FTP hostname (required — no default) |
required |
timeout
|
int
|
connection timeout in seconds |
DEFAULT_TIMEOUT
|
Source code in src/cdm_data_loaders/utils/file_transfer/ftp_client.py
get ¶
Return the FTP connection for the current thread, reconnecting if stale.
Sends a NOOP to verify the connection is still alive before returning it. If the server has closed the connection (e.g. after an idle timeout or session limit), the dead socket is discarded and a fresh connection is established transparently.
Source code in src/cdm_data_loaders/utils/file_transfer/ftp_client.py
close_all ¶
Close all thread-local FTP connections.
connect_ftp ¶
Connect and log in to an FTP server with TCP keepalive enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
FTP hostname |
required |
timeout
|
int
|
connection timeout in seconds |
DEFAULT_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
FTP
|
logged-in FTP connection |
Source code in src/cdm_data_loaders/utils/file_transfer/ftp_client.py
ftp_session ¶
Context manager yielding an active FTP connection and closing it on exit if created here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
FTP hostname |
required |
ftp
|
FTP | None
|
optional existing FTP connection (caller manages lifecycle when provided) |
None
|
timeout
|
int
|
connection timeout in seconds |
DEFAULT_TIMEOUT
|
Source code in src/cdm_data_loaders/utils/file_transfer/ftp_client.py
ftp_noop_keepalive ¶
Send NOOP if the connection has been idle longer than interval seconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ftp
|
FTP
|
active FTP connection |
required |
last_activity
|
float
|
monotonic timestamp of last FTP activity |
required |
interval
|
int
|
seconds of idle time before sending NOOP |
25
|
Returns:
| Type | Description |
|---|---|
float
|
updated last-activity timestamp |
Source code in src/cdm_data_loaders/utils/file_transfer/ftp_client.py
ftp_list_dir ¶
List files in an FTP directory with retry on transient errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ftp
|
FTP
|
active FTP connection |
required |
path
|
PurePosixPath
|
remote directory path |
required |
retries
|
int
|
number of retry attempts |
3
|
Returns:
| Type | Description |
|---|---|
list[PurePosixPath]
|
list of filenames |
Source code in src/cdm_data_loaders/utils/file_transfer/ftp_client.py
ftp_download_file ¶
ftp_download_file(
ftp: FTP,
remote_path: PurePosixPath,
local_path: Path,
retries: int = 3,
) -> None
Download a single file from FTP with retry on transient errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ftp
|
FTP
|
active FTP connection |
required |
remote_path
|
PurePosixPath
|
full remote file path |
required |
local_path
|
Path
|
local destination path |
required |
retries
|
int
|
number of retry attempts |
3
|
Source code in src/cdm_data_loaders/utils/file_transfer/ftp_client.py
ftp_retrieve_text ¶
Retrieve a text file from FTP, returning its content as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ftp
|
FTP
|
active FTP connection |
required |
remote_path
|
PurePosixPath
|
full remote file path |
required |
Returns:
| Type | Description |
|---|---|
str
|
file content |