An FTP and FTPS client: listing a directory, uploading and downloading files.
import ftp
This package connects to FTP servers, for uploading a site to shared hosting or pulling files off old equipment and suppliers' servers. Plain FTP sends the password and the data unencrypted; wherever the server supports it, use DialTLS, which encrypts both the commands and the data with AUTH TLS. When the server also speaks SSH, SFTP through ssh is the better choice: one encrypted connection, no separate data channel.
Transfers go in passive mode (EPSV, or PASV when the server lacks it), which works from behind a firewall; active mode (PORT) is not offered. The parsing functions, such as ParseListLine, are available for writing tools of your own too.
Things to know:
•
Call Binary before transfers, or a server in ASCII mode may change line endings inside images and archives.
•
Upload and Download hold the whole file in memory, and an interrupted transfer starts again from the beginning; there is no resume. Fine for site files and reports, not for multi-gigabyte backups.
•
Read, Write and Append carry a str, so they are for text: binary content stops at the first zero byte. Use Upload and Download for anything else.
•
Servers that require TLS session reuse on the data connection refuse every transfer; see the last section.
Publish a site to shared hosting
The usual job: connect with DialTLS, refuse to go on unless both the commands and the data are encrypted, log in, and upload a build directory with UploadDir. Switch to binary mode with Binary before any transfer, or some servers will change the line endings inside images and archives. Keeping the old index.html under another name with Rename gives you something to go back to.
This needs a real server, so it is only checked here. Every function that talks to the server leaves the reply in LastCode and LastReply, so after a false you can always print what the server said.
Fetch only the new files
List returns one FileInfo per entry with the name, the size and whether it is a directory or a link. Comparing that with what is already on disk turns a full download into a cheap sync, which suits a nightly job that collects reports from a supplier.
ftp-sync-down.salam
import ftp
import os
funcmain:
mut c := ftp.DialTLS("files.example.com", 21)
ifnot ftp.Ok(c) ornot ftp.Login(c, "reports", "app-password"):
println"cannot log in:", ftp.LastReply(c)
ret1end
_ := ftp.Binary(c)
_ := os.MkdirAll("reports")
mut fetched := 0mut skipped := 0each f in ftp.List(c, "/daily"):
if f.is_dir or f.is_link:
continueend
local := "reports/" + f.name
if os.Exists(local):
skipped++
continueendif ftp.Download(c, "/daily/" + f.name, local):
fetched++
else:
println"failed:", f.name, ftp.LastReply(c)
endendprintln"fetched", fetched, "new files, skipped", skipped
ftp.Quit(c)
ftp.Close(c)
ret0end
For a stricter check than "the file exists", compare f.size with the local size, or ModTime with the time you stored after the last run.
Read a directory listing yourself
FTP has no fixed format for listings; most servers send the same columns as ls -l. ParseListLine reads such a line into a FileInfo, which is useful when you get listings from somewhere else, such as a log or a ListRaw call, and want to work with them without a connection.
ftp-listing-report.salam
import ftp
importstrfuncmain:
raw := "drwxr-xr-x 2 ftp ftp 4096 Jan 01 2026 old\n-rw-r--r-- 1 ftp ftp 120034 Oct 02 08:10 sales-2026-10-01.csv\n-rw-r--r-- 1 ftp ftp 118702 Oct 03 08:10 sales-2026-10-02.csv\n-rw-r--r-- 1 ftp ftp 2251800 Oct 03 08:12 photos.zip\nlrwxrwxrwx 1 ftp ftp 20 Oct 03 08:12 latest.csv -> sales-2026-10-02.csv"mut total := 0asi64mut files := 0mut biggest := ftp.FileInfo {}
each line instr.Split(raw, "\n"):
f := ftp.ParseListLine(line)
if f.is_dir:
println"dir ", f.name
continueendif f.is_link:
println"link", f.name
continueendprintln"file", f.name, f.size, f.raw_date
files++
total += f.sizeif f.size > biggest.size:
biggest = f
endendprintln files, "files,", total, "bytes; biggest:", biggest.name
end
Output
dir old
file sales-2026-10-01.csv 120034 Oct 02 08:10
file sales-2026-10-02.csv 118702 Oct 03 08:10
file photos.zip 2251800 Oct 03 08:12
link latest.csv
3 files, 2490536 bytes; biggest: photos.zip
Servers that require TLS session reuse
Some FTPS servers insist that the data connection resumes the TLS session of the command connection. vsftpd does this with its default require_ssl_reuse=YES, and so do FileZilla Server and the Rebex test server. This package does not resume sessions yet, so on those servers logging in works but every listing and transfer is refused with reply 425:
ftp-public-demo.salam
import ftp
funcmain:
mut c := ftp.DialTLS("test.rebex.net", 21)
ifnot ftp.Ok(c) ornot ftp.Login(c, "demo", "password"):
println"cannot log in:", ftp.LastCode(c), ftp.LastReply(c)
ret1endprintln"encrypted:", ftp.Secure(c), ftp.SecureData(c)
println"system:", ftp.System(c)
entries := ftp.List(c, "/")
if entries.Len() == 0and ftp.LastCode(c) >= 400:
println"listing refused:", ftp.LastReply(c)
endeach f in entries:
println f.name, f.is_dir
end
ftp.Quit(c)
ftp.Close(c)
ret0end
If you run the server, set require_ssl_reuse=NO in vsftpd. If you do not, and the server offers SFTP, use ssh instead; fall back to plain Dial only on a network you trust, since it sends the password in the clear.
Which function for which job
Job
Use
Connect securely
DialTLS, or DialTLSWith for a private CA; Dial then AuthTLS does the same in two steps