Python实现ftp常用操作[ftplib]
admin
2023-07-31 00:35:59
0

使用到的模块 ftplib

代码托管位置 github-pytools

需求

快速进行ftp上传 ,下载,查询文件

原来直接在shell下操作:需要【连接,输用户名,输密码,单文件操作,存在超时限制】

太过于繁琐,容易操作失败

改进

一句命令,搞定多文件上传,下载,查询,列表等操作

后期可以加入更强大的功能

源代码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 #!/usr/bin/python# -*- coding:utf-8 -*-#ftp.py#    wklken@yeah.net#this script is used to do some operations more convenient via ftp  #1.[p]upload many files in the same time,show md5s  #2.[g]download many files in the same time,show md5s  #3.[l]list all the files on ftp site  #4.[f]search a file on ftp site,return True or Flase  #5.[h]show help info #add upload and download operations  20111210 version0.1#add md5sum after ops 20120308 version0.2 import sys,os,ftplib,socket CONST_HOST = \”ip\”CONST_USERNAME = \”username\”CONST_PWD = \”pwd\”CONST_BUFFER_SIZE = 8192 COLOR_NONE = \”33[m\”COLOR_GREEN = \”33[01;32m\”COLOR_RED = \”33[01;31m\”COLOR_YELLOW = \”33[01;33m\” def connect():    try:        ftp = ftplib.FTP(CONST_HOST)        ftp.login(CONST_USERNAME,CONST_PWD)        return ftp    except socket.error,socket.gaierror:        print(\”FTP is unavailable,please check the host,username and password!\”)        sys.exit(0) def disconnect(ftp):    ftp.quit() def upload(ftp, filepath):    f = open(filepath, \”rb\”)    file_name = os.path.split(filepath)[1]    try:        ftp.storbinary(\’STOR %s\’%file_name, f, CONST_BUFFER_SIZE)    except ftplib.error_perm:        return False    return True def download(ftp, filename):    f = open(filename,\”wb\”).write    try:        ftp.y\”>.write    try:        ftp.p>快速进行ftp上传 ,下载,查询文件

原来直接在shell下操作:需要【连接,输用户名,输密码,单文件操作,存在超时限制】

太过于繁琐,容易操作失败

改进

一句命令,搞定多文件上传,下载,查询,列表等操作

后期可以加入更强大的功能

源代码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 #!/usr/bin/python# -*- coding:utf-8 -*-#ftp.py#    wklken@yeah.net#this script is used to do some operations more convenient via ftp  #1.[p]upload many files in the same time,show md5s  #2.[g]download many files in the same time,show md5s  #3.[l]list all the files on ftp site  #4.[f]search a file on ftp site,return True or Flase  #5.[h]show help info #add upload and download operations  20111210 version0.1#add md5sum after ops 20120308 version0.2 import sys,os,ftplib,socket CONST_HOST = \”ip\”CONST_USERNAME = \”username\”CONST_PWD = \”pwd\”CONST_BUFFER_SIZE = 8192 COLOR_NONE = \”33[m\”COLOR_GREEN = \”33[01;32m\”COLOR_RED = \”33[01;31m\”COLOR_YELLOW = \”33[01;33m\” def connect():    try:        ftp = ftplib.FTP(CONST_HOST)        ftp.login(CONST_USERNAME,CONST_PWD)        return ftp    except socket.error,socket.gaierror:        print(\”FTP is unavailable,please check the host,username and password!\”)        sys.exit(0) def disconnect(ftp):    ftp.quit() def upload(ftp, filepath):    f = open(filepath, \”rb\”)    file_name = os.path.split(filepath)[1]    try:        ftp.storbinary(\’STOR %s\’%file_name, f, CONST_BUFFER_SIZE)    except ftplib.error_perm:        return False    return True def download(ftp, filename):    f = open(filename,\”wb\”).write    try:        ftp.6-38\”>383940

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
python清除字符串里非数字... 本文实例讲述了python清除字符串里非数字字符的方法。分享给大家供大家参考。具体如下: impor...