For Python2.7, the built-in FTP_TLS class does not support FTP server operations that implicit FTP over TLS encryption, and in order to support implicit FTP over TLS encryption, the built-in FTP_TLS class must be overloaded, The specific code is as follows:
ImportFtplibclassFtp_tls (ftplib. FTP_TLS):def __init__(Self, host="', user="', passwd="', acct="', Keyfile=none, Certfile=none, timeout=180): Ftplib. Ftp_tls.__init__(self, host, user, passwd, acct, keyfile, CertFile, timeout)defConnect (self, host="', Port=0, timeout=-999): ifHost! ="': Self.host=HostifPort >0:self.port=PortifTimeout! =-999: Self.timeout=TimeoutTry: Self.sock=socket.create_connection ((Self.host, Self.port), self.timeout) Self.af=self.sock.family Self.sock= Ssl.wrap_socket (Self.sock, Self.keyfile, Self.certfile, ssl_version=SSL. PROTOCOL_TLSV1) Self.file= Self.sock.makefile ('RB') Self.welcome=Self.getresp ()exceptException as E:PrintereturnSelf.welcomedefDownload (self, Remote_file_name, local_file_name): With open (Local_file_name,'WB') as Fp:self.retrbinary ('RETR%s'% Remote_file_name, fp.write)
The sample code for the download is as follows:
Host ='XXX'Port= 2222User='User'Password='Pass'Local_dir='./data'FTP=Ftp_tls () ftp.connect (host, Port) Log.debug ('begin to login') ftp.login (user, password) log.debug ('Login Succeed') Log.debug ('Welcome info =%s', Ftp.getwelcome ()) ftp.prot_p () ftp.cwd ('/crexbatch') Ftp.download ('People.zip','People.zip') Ftp.retrlines ('LIST') Fnames=Ftp.nlst () Open ('Crexbatch.txt','WB'). Write ('\ r \ n'. Join (Fnames))
Overloaded Python ftp_tls implementation implicit FTP over TLS mode download file