On this page
ftplib
Prev: fractions | Up: stdlib_module | Next: functools
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — ftplib
Tags: #public #python3 #stdlib
Import
import ftplib
Overview
FTP and explicit FTPS client (FTP, FTP_TLS) for listing remote directories, uploading (storbinary), and downloading (retrbinary) files. Use ftplib when automating transfers with legacy FTP/FTPS endpoints; for SFTP (SSH File Transfer Protocol), use paramiko or asyncssh instead.
Quick Start
import ftplib
import io
def download_bytes_ftps(host: str, user: str, credential: str, remote_path: str) -> bytes:
buf = io.BytesIO()
with ftplib.FTP_TLS(host, timeout=30) as ftp:
ftp.login(user, credential)
ftp.prot_p() # encrypt the data connection, not just control/login
ftp.retrbinary(f"RETR {remote_path}", buf.write)
return buf.getvalue()
Implementation Walkthrough
1. Uploading Files and Listing Structured Metadata
Use storbinary() with a binary file object for uploads, and prefer mlsd() over nlst()/dir() when the server supports RFC 3659 machine-readable listings:
import ftplib
from pathlib import Path
def upload_file(ftp: ftplib.FTP_TLS, local_path: Path, remote_name: str) -> None:
with local_path.open("rb") as fh:
ftp.storbinary(f"STOR {remote_name}", fh)
Gotchas & Boundaries
- FTPS ≠ SFTP:
FTP_TLSis FTP over TLS (ports 21/990). It cannot connect to SSH/SFTP servers (port 22). - Always call
ftp.prot_p()withFTP_TLS:login()secures the control channel, but file transfers stay unencrypted untilprot_p()switches the data channel to protected mode. - Passive mode is the default (
set_pasv(True)): Keep passive mode enabled behind NAT/firewalls; active mode usually fails unless inbound ports are forwarded. - Avoid plain
FTPon untrusted networks: Credentials and file contents travel in cleartext without TLS.
Classes & Objects
- ftplib.error_perm —
ftplib.error_permclass. (Methods:.add_note(),.with_traceback()) - ftplib.error_proto —
ftplib.error_protoclass. (Methods:.add_note(),.with_traceback()) - ftplib.error_reply —
ftplib.error_replyclass. (Methods:.add_note(),.with_traceback()) - ftplib.error_temp —
ftplib.error_tempclass. (Methods:.add_note(),.with_traceback()) - ftplib.FTP — An FTP client class. (Methods:
.abort(),.acct(),.close(),.connect(),.cwd(),.debug()) - ftplib.FTP_TLS — A FTP subclass which adds TLS support to FTP as described in RFC-4217. (Methods:
.abort(),.acct(),.auth(),.ccc(),.close(),.connect())
Constants & Attributes
ftplib.all_errors(tuple) =(<class 'ftplib.Error'>, <class 'OSError'>, <class 'EOFEr...
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/fractions
- tuts/python3/stdlib_module/ftplib/error_perm
- tuts/python3/stdlib_module/ftplib/error_proto
- tuts/python3/stdlib_module/ftplib/error_reply
- tuts/python3/stdlib_module/ftplib/error_temp
- tuts/python3/stdlib_module/ftplib/FTP
- tuts/python3/stdlib_module/ftplib/FTP_TLS
- tuts/python3/stdlib_module/functools
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/fractions Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ftplib/error_perm Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ftplib/error_proto Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ftplib/error_reply Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ftplib/error_temp Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ftplib/FTP Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ftplib/FTP_TLS Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/functools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib