标签:use family deb dir import ever 端口 style welcome
Python中的ftplib模块
Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件
FTP的工作流程及基本操作可参考协议RFC959
ftp登陆连接
from ftplib import FTP #加载ftp模块
ftp相关命令操作
1 from ftplib import FTP 2 3 4 def ftpconnect(host, username, password): 5 ftp = FTP() 6 ftp.set_debuglevel(2) 7 ftp.connect(host, 21) 8 ftp.login(username, password) 9 return ftp 10 11 12 def downloadfile(ftp, remotepath, localpath): 13 # 从ftp下载文件 14 bufsize = 1024 15 fp = open(localpath, ‘wb‘) 16 ftp.retrbinary(‘RETR ‘ + remotepath, fp.write, bufsize) 17 ftp.set_debuglevel(0) 18 fp.close() 19 20 21 def uploadfile(ftp, localpath, remotepath): 22 # 从本地上传文件到ftp 23 bufsize = 1024 24 fp = open(localpath, ‘rb‘) 25 ftp.storbinary(‘STOR ‘ + remotepath, fp, bufsize) 26 ftp.set_debuglevel(0) 27 fp.close() 28 29 if __name__ == "__main__": 30 ftp = ftpconnect("10.1.2.254", "download", "download") 31 uploadfile(ftp, "/home/rxf/other/PlatServerTest/2.jpg", "D:/download/Ranxiangfei/2.jpg") 32 downloadfile(ftp, "D:/download/Ranxiangfei/svn/V2.0.xxx.171030_Alpha/xzrs.tar.gz", 33 "/home/rxf/svn/V2.0.xxx.171030_Alpha/xzrs.tar.gz") 34 35 ftp.quit()
标签:use family deb dir import ever 端口 style welcome
原文地址:http://www.cnblogs.com/ranxf/p/7771672.html