{"id":492,"date":"2013-06-09T10:00:23","date_gmt":"2013-06-09T02:00:23","guid":{"rendered":"http:\/\/www.kiccleaf.com\/?p=492"},"modified":"2013-06-09T10:00:23","modified_gmt":"2013-06-09T02:00:23","slug":"python%e5%ae%9e%e7%8e%b0ftp%e7%ae%80%e5%8d%95%e4%b8%8a%e4%bc%a0%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6%e3%80%90%e8%bd%ac%e3%80%91","status":"publish","type":"post","link":"http:\/\/www.kiccleaf.com\/?p=492","title":{"rendered":"python\u5b9e\u73b0FTP\u7b80\u5355\u4e0a\u4f20\u4e0b\u8f7d\u6587\u4ef6\u3010\u8f6c\u3011"},"content":{"rendered":"<p>\u53c2\u8003\u4e86\u811a\u672c\uff0cPython\u7f16\u5199\u4e0a\u4f20\u4e0b\u8f7d\u8fd8\u662f\u5f88\u65b9\u4fbf\u7684\uff0c\u641c\u7d22\u4e86\u4e00\u4e0b\u8d44\u6599\u627e\u5230\u4e86\u4e00\u54e5\u4eec\u7684\u53c2\u8003\u4f8b\u5b50\uff0c\u8f6c\u6765\u5148\uff01<br \/>\n\u7ef4\u62a4\u670d\u52a1\u5668\u7684\u670b\u53cb\u90fd\u5f88\u6e05\u695a\uff0c\u7ecf\u5e38\u9700\u8981\u5907\u4efd\u4e0a\u4f20\u4e0b\u8f7d\u5230\u5176\u4ed6\u670d\u52a1\u5668\uff0c\u6240\u4ee5\u4eba\u53bb\u505a\u5c31\u5f88\u8d39\u65f6\u95f4\uff0c\u6539\u7528\u7f16\u5199\u811a\u672c\u6bcf\u5929\u51cc\u6668\u591c\u6df1\u4eba\u9759\u7684\u65f6\u5019\u8ba9\u5b83\u81ea\u5df1\u8dd1\u53bb\uff01<br \/>\nWin\u7684\u5199\u6cd5\u662f\u91c7\u7528BAT\u6587\u4ef6\u6765\u5b9e\u73b0\uff0cLinux\u548cFreeBSD\u4e0b\u53ef\u4ee5\u91c7\u7528Shell\u547d\u4ee4\uff0c\u4f46\u53d1\u73b0Python\u66f4\u5f3a\u5927\uff01\u4ee5\u4e0b\u662f\u8f6c\u6765\u7684\u5217\u5b50\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\n#!\/usr\/bin\/env python \r\n# -*- coding: utf-8 -*- \r\n \r\nfrom ftplib import FTP \r\n \r\ndef ftp_up(filename = &quot;20120904.rar&quot;): \r\n    ftp=FTP() \r\n    ftp.set_debuglevel(2)#\u6253\u5f00\u8c03\u8bd5\u7ea7\u522b2\uff0c\u663e\u793a\u8be6\u7ec6\u4fe1\u606f;0\u4e3a\u5173\u95ed\u8c03\u8bd5\u4fe1\u606f \r\n    ftp.connect('192.168.0.1','21')#\u8fde\u63a5 \r\n    ftp.login('admin','admin')#\u767b\u5f55\uff0c\u5982\u679c\u533f\u540d\u767b\u5f55\u5219\u7528\u7a7a\u4e32\u4ee3\u66ff\u5373\u53ef \r\n    #print ftp.getwelcome()#\u663e\u793aftp\u670d\u52a1\u5668\u6b22\u8fce\u4fe1\u606f \r\n    #ftp.cwd('xxx\/xxx\/') #\u9009\u62e9\u64cd\u4f5c\u76ee\u5f55 \r\n    bufsize = 1024#\u8bbe\u7f6e\u7f13\u51b2\u5757\u5927\u5c0f \r\n    file_handler = open(filename,'rb')#\u4ee5\u8bfb\u6a21\u5f0f\u5728\u672c\u5730\u6253\u5f00\u6587\u4ef6 \r\n    ftp.storbinary('STOR %s' % os.path.basename(filename),file_handler,bufsize)#\u4e0a\u4f20\u6587\u4ef6 \r\n    ftp.set_debuglevel(0) \r\n    file_handler.close() \r\n    ftp.quit() \r\n    print &quot;ftp up OK&quot; \r\n \r\ndef ftp_down(filename = &quot;20120904.rar&quot;): \r\n    ftp=FTP() \r\n    ftp.set_debuglevel(2) \r\n    ftp.connect('192.168.0.1','21') \r\n    ftp.login('admin','admin') \r\n    #print ftp.getwelcome()#\u663e\u793aftp\u670d\u52a1\u5668\u6b22\u8fce\u4fe1\u606f \r\n    #ftp.cwd('xxx\/xxx\/') #\u9009\u62e9\u64cd\u4f5c\u76ee\u5f55 \r\n    bufsize = 1024 \r\n    filename = &quot;20120904.rar&quot; \r\n    file_handler = open(filename,'wb').write #\u4ee5\u5199\u6a21\u5f0f\u5728\u672c\u5730\u6253\u5f00\u6587\u4ef6 \r\n    ftp.retrbinary('RETR %s' % os.path.basename(filename),file_handler,bufsize)#\u63a5\u6536\u670d\u52a1\u5668\u4e0a\u6587\u4ef6\u5e76\u5199\u5165\u672c\u5730\u6587\u4ef6 \r\n    ftp.set_debuglevel(0) \r\n    file_handler.close() \r\n    ftp.quit() \r\n    print &quot;ftp down OK&quot; \r\n<\/pre>\n<p>\u8f6c\u81ea\uff1ahttp:\/\/wangwei007.blog.51cto.com\/68019\/983638<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\u4e86\u811a\u672c\uff0cPython\u7f16\u5199\u4e0a\u4f20\u4e0b\u8f7d\u8fd8\u662f\u5f88\u65b9\u4fbf\u7684\uff0c&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"http:\/\/www.kiccleaf.com\/?p=492\">Continue reading<span class=\"screen-reader-text\">python\u5b9e\u73b0FTP\u7b80\u5355\u4e0a\u4f20\u4e0b\u8f7d\u6587\u4ef6\u3010\u8f6c\u3011<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6,"footnotes":""},"categories":[20],"tags":[111,95],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category-python","tag-ftp","tag-python","entry"],"views":4627,"_links":{"self":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=492"}],"version-history":[{"count":0,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts\/492\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}