Overloaded Python ftp_tls implementation implicit FTP over TLS mode download file

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.