Python中ftplib模块应用的实例是怎样的
Admin 2022-08-17 群英技术资讯 751 次浏览
这篇文章给大家介绍了“Python中ftplib模块应用的实例是怎样的”的相关知识,讲解详细,步骤过程清晰,有一定的借鉴学习价值,因此分享给大家做个参考,感兴趣的朋友接下来一起跟随小编看看吧。Python中默认安装的ftplib模块定义了FTP类,可用来实现简单的ftp客户端,用于上传或下载文件。
from ftplib import FTP # 加载ftp模块
ftp = FTP() # 设置变量
ftp.set_debuglevel(2) # 打开调试级别2,显示详细信息
ftp.connect("10.126.64.14", 21) # 连接的ftp sever和端口
ftp.login("usr_esop", "PWD4ftp@201903#") # 连接的用户名,密码
print(ftp.getwelcome()) # 打印出欢迎信息
ftp.cwd("/ZS_ESOP/55-548410/-/zh/Split") # 更改远程目录
bufsize = 1024 # 设置的缓冲区大小
filename = "tslint.json" # 需要下载的文件
file_handle = open(filename, "wb").write # 以写模式在本地打开文件
ftp.retrbinary("RETR tslint.json", file_handle, bufsize) # 接收服务器上文件并写入本地文件
ftp.set_debuglevel(0) # 关闭调试模式
ftp.quit # 退出ftp
ftp.cwd(pathname) #设置FTP当前操作的路径,cwd可以使用“..”,但不使用"./path"以及"../path"这样的相对路径
ftp.dir() #显示目录下文件信息
ftp.nlst() #获取目录下的文件
ftp.mkd(pathname) #新建远程目录
ftp.pwd() #返回当前所在位置
ftp.rmd(dirname) #删除远程目录
ftp.delete(filename) #删除远程文件
ftp.rename(fromname, toname)#将fromname修改名称为toname。可以带路径,起到移动文件的作用
ftp.storbinaly("STOR filename.txt",file_handel,bufsize) #以
BINARY
模式上传目标文件
ftp.storlines("STOR filename.txt",file_handel) #以ASCII模式存储文件
ftp.retrbinary("RETR filename.txt",file_handel,bufsize)#以
BINARY模式
下载FTP文件
FTP.retrlines("RETR filename.txt",file_handel) #以ASCII模式获取文件或者文件夹列表
from ftplib import FTP
def ftpconnect(host, username, password):
ftp = FTP()
# ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
ftp.encoding = 'GB2312' # 解决中文编码问题,默认是latin-1,如果将 encoding='utf-8'报错,可以 改为GB2312、gbk、ISO-8859-1
ftp.connect(host, 21) # 连接
ftp.login(username, password) # 登录,如果匿名登录则用空串代替即可
return ftp
def downloadfile(ftp, remotepath, localpath):
bufsize = 1024 # 设置缓冲块大小
fp = open(localpath, 'wb') # 以写模式在本地打开文件
ftp.retrbinary('RETR ' + remotepath, fp.write, bufsize) # 接收服务器上文件并写入本地文件
ftp.set_debuglevel(0) # 关闭调试
fp.close() # 关闭文件
def uploadfile(ftp, remotepath, localpath):
bufsize = 1024
fp = open(localpath, 'rb')
ftp.storbinary('STOR ' + remotepath, fp, bufsize) # 上传文件
ftp.set_debuglevel(0)
fp.close()# 关闭文件
if __name__ == "__main__":
ftp = ftpconnect("******", "***", "***")
downloadfile(ftp, "***", "***")
uploadfile(ftp, "***", "***")
ftp.quit()
注:目录内为文件,若为目录则无法传输
import os
import ftplib
class myFtp:
ftp = ftplib.FTP()
bIsDir = False
path = ""
def __init__(self, host, port='21'):
# self.ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
# self.ftp.set_pasv(0) #0主动模式 1 #被动模式
self.ftp.encoding = 'GB2312' # 解决中文编码问题,默认是latin-1,如果将 encoding='utf-8'报错,可以 改为GB2312、gbk、ISO-8859-1
self.ftp.connect(host, port)
def login(self, user, passwd):
self.ftp.login(user, passwd)
print(self.ftp.welcome)
def down_load_file(self, local_file, remote_file):
file_handler = open(local_file, 'wb')
self.ftp.retrbinary("RETR %s" % remote_file, file_handler.write)
file_handler.close()
return True
def up_load_file(self, local_file, remote_file):
if not os.path.isfile(local_file):
return False
file_handler = open(local_file, "rb")
self.ftp.storbinary('STOR %s' % remote_file, file_handler, 4096)
file_handler.close()
return True
def up_load_file_tree(self, local_dir, remote_dir):
if not os.path.isdir(local_dir):
return False
# print("local_dir:", local_dir)
local_names = os.listdir(local_dir)
# print("list:", local_names)
# print(remote_dir)
self.ftp.cwd(remote_dir)
for Local in local_names:
src = os.path.join(local_dir, Local)
if os.path.isdir(src):
self.up_load_file_tree(src, Local)
else:
self.up_load_file(src, Local)
self.ftp.cwd("..")
return
def down_load_file_tree(self, local_dir, remote_dir):
print("remote_dir:", remote_dir)
if not os.path.isdir(local_dir):
os.makedirs(local_dir)
self.ftp.cwd(remote_dir)
remote_names = self.ftp.nlst()
# print("remote_names", remote_names)
# print(self.ftp.nlst(remote_dir))
for file in remote_names:
local = os.path.join(local_dir, file)
if self.is_dir(file):
self.down_load_file_tree(local, file)
else:
self.down_load_file(local, file)
self.ftp.cwd("..")
return
def show(self, list):
result = list.lower().split(" ")
if self.path in result and "
" in result:
self.bIsDir = True
def is_dir(self, path):
self.bIsDir = False
self.path = path
# this ues callback function ,that will change bIsDir value
self.ftp.retrlines('LIST', self.show)
return self.bIsDir
def close(self):
self.ftp.quit()
if __name__ == "__main__":
ftp = myFtp('10.126.64.14',21)
ftp.login("usr_esop", "PWD4ftp@201903#")
ftp.down_load_file_tree(r"C:\PycharmProjects\untitled\backup", "/ZS_ESOP/55-548410/-/zh/Split") # ok
ftp.up_load_file_tree(r"C:\PycharmProjects\untitled\backup", "/ZS_ESOP/55-548410/-/zh/Split") # ok
ftp.close()
print("ok!")
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家介绍了Python内建类型str的源码学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要为大家详细介绍了python实现名片管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
大家好,本篇文章主要讲的是python用turtle库绘画圣诞树,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
这篇文章主要为大家介绍了python神经网络Densenet模型复现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要和大家分享三个Python中提取人脸特征的方法,文中的示例代码讲解详细,对我们学习Python有一定的帮助,需要的可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008