#!/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" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" οnclick="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>
python连接ftp上传下载
最新推荐文章于 2024-05-27 14:50:58 发布