python ftp 下載

來源:互聯網
上載者:User

剛學習了《python核心編程》的第十七章,ftp編程,寫了一個小指令碼下載ftp網站上的全部檔案,還沒學習多線程編程,後面要給加上多線程下載。

from ftplib import FTPimport reimport oshost = 'ftp.neu.edu.cn'dir_path = '/ebook/python'def get_filename(string):    patt = re.compile(r'2011\s(.+)')    filename = re.findall(patt, string)[0]    return filename    def main():    filenames = []    try:        fp = FTP(host)    except Exception:        print "can not connect to ",host        return     print 'connect to ',host,' successfully'    try:        fp.login()    except Exception:        print 'login failed'        return     print 'login successfully'        try:        fp.cwd(dir_path)    except Exception:        print 'change to directory:',dir_path," failed"        fp.quit()        return    print 'change to directory %s successfully' %(fp.pwd())        fp.retrlines('LIST',lambda x:filenames.append(get_filename(x)))    """for filename in filenames:        print filename    return     """    for filename in filenames:        try:            fp.retrbinary('RETR %s' %filename, open(filename,'wb').write)        except Exception ,e:            print e        print '%s download successfully' % filename    fp.quit()    print 'all file download successfully'if __name__ == "__main__":    main()    

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.