python連線ftp上傳下載
阿新 • • 發佈:2019-02-20
#!/usr/bin/python #-*- coding: utf-8 -*- from ftplib import FTP def ftpconnect() ftp_server = 'ftp.python.org' username = '*****' password = ******'' ftp=FTP() ftp.set_debuglevel(2) #開啟除錯級別2,顯示詳細資訊 ftp.connect(ftp_server,21) #連線 ftp.login(username,password) #登入,如果匿名登入則用空串代替即可 return ftp def downloadfile() remotepath = "/home/pub/dog.jpg"; ftp = ftpconnect() print ftp.getwelcome() #顯示ftp伺服器歡迎資訊 bufsize = 1024 #設定緩衝塊大小 localpath = 'f:\\test\\dog.jpg' fp = open(localpath,'wb') #以寫模式在本地開啟檔案 ftp.retrbinary('RETR ' + remotepath,fp.write,bufsize) #接收伺服器上檔案並寫入本地檔案 ftp.set_debuglevel(0) #關閉除錯 fp.close() ftp.quit() #退出ftp伺服器 <br> def uploadfile() remotepath = "/home/pub/dog.jpg" ftp = ftpconnect() bufsize = 1024 localpath = 'f:\\test\\dog.jpg' fp = open(localpath,'rb') ftp.storbinary('STOR '+ remotepath ,fp,bufsize) #上傳檔案 ftp.set_debuglevel(0) fp.close() #關閉檔案 ftp.quit() <br> <br> <div class="dp-highlighter bg_python"><div class="bar"><div class="tools"><b>[python]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-py"><li class="alt"><span><span> </span></span></li></ol></div><pre name="code" class="python" style="display: none;"></pre> <pre></pre> <pre></pre>