Connect() Method Of FTP Class In Python

Method Name:

connect

 

Method Signature:

connect(host=””, port =21, timeout=None, source_address=None)

Parameters:

Host – The IP address or the host name of the FTP server to connect to.

Port – The port number at which the FTP server is listening. The standard port number at which FTP servers listen is 21.

timeout – Number of seconds to block till the connection is made. If timeout seconds are elapsed the connect method returns.

source_address A tuple consisting of ip address/host name and port number of the client.

 

Method overview:

  • The method connect() establishes a connection to an FTP server with the name/ip address specified by host parameter. 
  • Since the constructor FTP() as well takes parameters like hostname, user name and so on, it is only required to call this method if a connection was not already established through the FTP constructor.

Example:

# Example Python program that uses connect() method of ftplib.FTP class

# to connect to an FTP server

from ftplib import FTP

 

# Name of the server

serverName = "apublicserverthatallowstesting.com";

 

# Create an FTP instance

ftpObject = FTP();

 

# Connect to the FTP server

ftpResponseCode = ftpObject.connect(host=serverName);

print("Response code for connect:%s"%ftpResponseCode)

 

# Do an anonymous login

ftpResponseCode = ftpObject.login();

print("Response code for login:%s"%ftpResponseCode)

 

# Issue the FTP command LIST to get the information on files present in

# the current working directory

ftpResponseCode = ftpObject.retrlines("LIST", print);

print("Response code for command LIST:%s"%ftpResponseCode);

 

Output:

Response code for connect:220 (vsFTPd 3.0.3)

Response code for login:230 Login successful.

-rw-r--r--    1 0        0        1024 Feb 19  2016 1.txt

-rw-r--r--    1 0        0        2048 Feb 19  2016 2.txt

drwxr-xr-x    2 105      108     561152 Sep 26 11:21 upload

Response code for command LIST:226 Directory send OK.

 

 

 


Copyright 2022 © pythontic.com