Login() method of FTP class in Python

Method Name:

login

Method Signature:

login(user='', passwd='', acct='')

Return Value:

An FTP 230 Login successful message, upon successful login or a failure message like 530 This FTP server is anonymous only.

Parameters:

user – The user name of the FTP account on the server.  The default value is an empty string. When no value is specified, the user parameter is assigned internally a default value of ‘anonymous’ by the login() function.

passwd – Password corresponding to the user name. The default value is an empty string. When the user name is anonymous and the password is an empty string or a hyphen, the password is internally assigned to the value ‘anonymous@’ by the login() function.

acct – Accounting information.

 

Overview:

  • The login() method of FTP class, initiates a logged-in session with the FTP server for a user.
  • The login() method sends the user credentials to the FTP server. Once the FTP server completes the user authentication, it creates an FTP session.
  • The login() function roughly translates into issuing of FTP commands: USER and PASS.
  • For the login() to work, the connection to the FTP server should have been already established either through the constructor - FTP() or through the method connect().

 

Example:

# Example Python program that logs in as an anonymous user

from ftplib import FTP

 

# Create an FTP object

ftpObject = FTP();

 

# Connect to the FTP server

connectResponse = ftpObject.connect(host="someserverthatallowstesting");

print(connectResponse);

 

# Login to the FTP server

loginResponse = ftpObject.login();

print(loginResponse);

 

 

Output:

220 (vsFTPd 3.0.3)

230 Login successful.

 


Copyright 2026 © pythontic.com