• 重载Python FTP_TLS 实现Implicit FTP Over TLS方式下载文件


    对于Python2.7来说,内置的FTP_TLS类并不支持Implicit FTP Over TLS加密方式的FTP Server操作,为支持Implicit FTP Over TLS加密方式,必须重载内置的FTP_TLS类,具体代码如下:

    import ftplib
    
    class FTP_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)
        def connect(self, host='', port=0, timeout=-999):
            if host != '':
                self.host = host
            if port > 0:
                self.port = port
            if timeout != -999:
                self.timeout = timeout
    
            try:
                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()
            except Exception as e:
                print e
            return self.welcome
        def download(self, remote_file_name, local_file_name):
            with open(local_file_name, 'wb') as fp:
                self.retrbinary('RETR %s' % remote_file_name, fp.write)

    下载的示例代码如下所示:

        host = 'XXX'
        port = 2222
        user = '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('
    '.join(fnames))
  • 相关阅读:
    用电脑给手机安装App
    切换皮肤的实现
    瀑布流的简单实现
    HTML5的实用
    HTML5的特性,发展,及使用
    录音的使用步骤
    支付宝集成步骤
    美团(iPad)顶部界面的简单实现, 及开发时常见bug
    真机调试/打包测试/程序发布/内购的具体操作流程
    IOS 触摸事件的处理
  • 原文地址:https://www.cnblogs.com/Jerryshome/p/4801066.html
Copyright © 2020-2023  润新知