1. Home
  2. Data & files

Data files · FTP

Opening an FTP Connection: Anonymous and Password Login

Eigenfield editorial team ·

Opening an FTP connection means more than naming a server. The protocol uses two kinds of channel, and a script that understands the difference will recover from failures that otherwise look mysterious. This page explains the connection sequence, the choice between anonymous and named logins, and the settings that make an unattended script dependable. Listing and downloading come afterwards; see listing files on an FTP server. The topic is one of several under Data and files.

Two channels, not one

FTP opens a control channel first, normally on port 21. Commands and short replies travel over it as plain text lines: a user name, a password, a request to change directory. Each reply starts with a three-digit code. Codes beginning with 2 signal success, 3 asks for more information, 4 reports a temporary failure, and 5 reports a permanent one.

Every directory listing and file transfer then uses a separate data channel, created for that one operation and closed afterwards. Who opens it decides the mode. In active mode the server connects back to the client, which a firewall or home router usually blocks. In passive mode the server announces a port and the client connects to it, which works through most networks. When a login succeeds but the first listing hangs, the data channel is nearly always the cause, and switching to passive mode fixes it.

Anonymous and named logins

Public servers often accept the user name anonymous. By long convention the password is an e-mail address, though most servers accept any text. Anonymous accounts are read-only in practice and expose only a public area. A script that fetches public data should say so in its configuration: anonymous login, no stored secret.

Named accounts need a user and a password, and the same rule about credentials applies as for databases: take them from an environment variable or a protected file, never from the script text, and give the account the narrowest rights that do the job.

from ftplib import FTP
ftp = FTP()
ftp.connect("ftp.example.com", 21, timeout=30)
ftp.set_pasv(True)
ftp.login("anonymous", "[email protected]")
print(ftp.getwelcome())

The welcome line printed at the end is a cheap sanity check: it confirms that the control channel works and often names the server software, which helps when interpreting unusual listing formats.

Settings that prevent hangs

A network call without a timeout can block a script forever. Set a timeout of a few tens of seconds on the connection, and set one on the data transfer as well if the library lets you. Add a bounded retry: try three times with a growing pause, then give up with a clear message. Retrying forever hides an outage, and retrying instantly hammers a server that is already struggling.

Idle connections are dropped by many servers after a few minutes. A long job that opens a connection, spends ten minutes computing, and then tries to use it will find it closed. Open the connection late, as close as possible to the first transfer, or send a harmless command now and then to keep it alive.

Encrypted variants

Plain FTP transmits the password and every byte of data in the clear. Two encrypted alternatives exist: FTP over TLS, which keeps the same commands inside an encrypted session, and a separate file-transfer protocol that runs over SSH and shares only the name with FTP. Where a server offers either, use it. Which one a given server speaks is an administrative fact to look up, not something to guess.

Closing properly

Send the quit command and close the socket, even after an error. Wrap the work in a try-finally block, the pattern shown under trapping errors in scripts, so that the connection is released whatever happens. A script that leaks connections will eventually be refused by the server, usually at the worst time.

Back to top