import ftplib import socket import re import time ftp = ftplib.FTP() ftp.set_debuglevel(1) ftp.connect('ftp.debian.org', timeout=10) ftp.login('anonymous','user@example.com') ftp.sendcmd('TYPE A') print("\n") print("Test sending of string (UTF-8) in cmd line") ftp.sendcmd('NOOP') print("Test OK\n") #needed to request non UTF-8 file names exactly as they came in LIST data print("Test sending of bytes (Latin-1) in cmd line") ftp.sendcmd(b'NOOP') print("Test OK\n") print("Test return of last status and command/response order") s = ftp.transfercmd('LIST') fp = s.makefile('r') fp.read() fp.close() #socket reached EOF; we don't know if transfer was complete at this point or server error occurred s.close() #we must now wait for the transfer status code on the command socket #the server might respond with some delay ftp.clear_last_status() #clear old status code = None msg = "" while (code == None): code, msg = ftp.get_last_status() time.sleep(0.1) print("Last status: %s %s" % (str(code), str(msg))) ftp.sendcmd('NOOP') code, msg = ftp.get_last_status() print("Last status: %s %s" % (str(code), str(msg))) assert (code == 200) print("Test OK\n") ''' #manual transfer socket - should result in same behaviour r = ftp.sendcmd('EPSV') r = re.search('\|([0-9]+)\|', r) port = int( r.group(1) ) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('ftp.debian.org', port)) ftp.sendcmd('LIST') ''' print("All tests OK") ftp.close()