2.5 定义FTP工具的各种方法
用class定义ftp工具的各种方法
import os,sys
from ftplib import FTP
from mimetypes import guess_type,add_type
from getpass import getpass #定义默认配置
dfltSite = '192.168.191.1'
dfltUser = ()
dfltRdir = '.' class FtpTools: #allows these 3 to be redefined
def getlocaldir(self):
return (len(sys.argv) > 1 and sys.argv[1]) or '.' #返回本地路径 def getcleanall(self):
return input( 'Clean local directory first? ')[:1] in ['y','Y'] #是否清除所有
#暂且不用
# def getpassword(self):
# print('password')
# return getpass('Password for %s on %s'%(self.remoteuser,self.remotesite)) #键入密码
#配置
def configTransfer(self,site=dfltSite,rdir=dfltRdir,user=dfltUser):
self.nonpassive = False # passive FTP by default
self.remotesite = site
self.remotedir = rdir # FTP的路径
self.remoteuser = user # 因为我没设置密码,所以为空集 self.localdir =self.getlocaldir()
self.cleanall = self.getcleanall()
self.remotepass = '' #密码
#判断文件格式
def isTextKind(self,remotename,trance=True):
''' use mimetypes to guess if filanme means text or binary''' add_type('text/x-python-win','.pyw') #not in tables
mimetype, encoding = guess_type(remotename,strict=False) #allow extras
mimetype = mimetype or '?/?'
mimetype = mimetype.split('/')[0]
if trance: print(mimetype,encoding or '')
return mimetype == 'text' and encoding == None #连接服务器
def connectFtp(self): # 连接PFTP
print('connecting...')
connection = FTP(self.remotesite)
connection.login(self.remoteuser,self.remotepass)
connection.cwd(self.remotedir) #most do passive
if self.nonpassive:
connection.set_pasv(False)
self.connection = connection
#清除本地文件
def cleanLocals(self):
'''try to delete all local files first to remove garbage''' #清除本地文件 if self.cleanall:
for localname in os.listdir(self.localdir):
try:
print('deleting local', localname)
os.remove(os.path.join(self.remotedir, localname))
except:
print('cannot delete', localname)
#清除远程文件
def cleanRemotes(self):
'''try to delete all remote files to remove garbage''' if self.cleanall:
for remotename in self.connection.nlst():
try:
print('deleting local', remotename)
self.connection.delete(remotename)
except:
print('cannot delete', remotename) #下载文件
def downloadOne(self,remotename,localpath):
if self.isTextKind(remotename):
localfile = open(localpath,'w',encoding=self.connection.encoding)
def callback(line): localfile.write(line + '\n')
self.connection.retrlines('RETR '+remotename,callback)
else:
localfile = open(localpath,'wb')
self.connection.retrbinary('RETR ' + remotename, localfile.write)
localfile.close() #上传文件
def uploadOne(self, localname,remotename, localpath):
if self.isTextKind(localname):
localfile = open(localpath,'rb')
self.connection.storlines('RETR ' + remotename, localfile) else:
localfile = open(localpath, 'rb')
self.connection.storbinary('RETR '+remotename,localfile)
localfile.close() #下载目录
def downloadDir(self):
remotefiles = self.connection.nlst()
for remotename in remotefiles:
if remotename in ('.', '..') or not '.' in remotename: continue # 判断是否目录,这里根据实际情况更改
localpath = os.path.join(self.localdir, remotename)
print('downing', remotename, 'to', localpath, 'as',end=' ')
self.downloadOne(remotename,localpath)
print('Done',len(remotefiles),'files downloaded') #上传目录
def uploadDir(self):
localfiles = os.listdir(self.localdir)
for localname in localfiles:
localpath = os.path.join(self.localdir, localname)
print('uploading', localpath, 'to', localname, 'as',end=' ')
self.uploadOne(localname,localpath,localname)
print('Done',len(localfiles),'files uploaded') def run(self,cleanTarget=lambda:None, transferAct=lambda:None): self.connectFtp()
cleanTarget()
transferAct()
self.connection.quit() if __name__ == '__main__':
ftp = FtpTools()
xfermode = 'download'
if len(sys.argv) > 1:
xfermode = sys.argv.pop(1)
if xfermode == 'download':
ftp.configTransfer()
ftp.run(cleanTarget=ftp.cleanLocals, transferAct=ftp.downloadDir)
if xfermode == 'upload':
ftp.configTransfer(site='192.168.191.1') #根据自己情况更改IP
ftp.run(cleanTarget=ftp.cleanRemotes, transferAct=ftp.uploadDir)
else:
print('Usage: ftptools.py["download/upload"] [loacldir] ')
2.5 定义FTP工具的各种方法的更多相关文章
- Linux环境下FTP工具的使用方法
在Windows环境下创建Ftp目录作为服务器根目录 在Linux端的操作: 从服务器端下载文件到Linux端: ftpget -u User -p Password ServerIP File Fi ...
- java:工具(汉语转拼音,压缩包,EXCEL,JFrame窗口和文件选择器,SFTP上传下载,FTP工具类,SSH)
1.汉语转拼音: import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuP ...
- 深入理解为什么Java中方法内定义的内部类可以访问方法中的局部变量
好文转载:http://blog.csdn.net/zhangjg_blog/article/details/19996629 开篇 在我的上一篇博客 深入理解Java中为什么内部类可以访问外部类的成 ...
- 详述Linux ftp命令的使用方法
转自:http://os.51cto.com/art/201003/186325.htm ftp服务器在网上较为常见,Linux ftp命令的功能是用命令的方式来控制在本地机和远程机之间传送文件,这里 ...
- windows快速搭建FTP工具Serv-U FTP Server
本文介绍一个简单的FTP工具,当然windows系统自带FTP工具,但是配置方法没有第三方工具来的简单可操作性好. 此工具用于搭建FTP环境,对于需要测试FTP上传功能具有极大帮助.例如球机抓拍图片上 ...
- 翻译:Laravel-4-Generators 使用自己定义代码生成工具高速进行Laravel开发
使用自己定义代码生成工具高速进行Laravel开发 这个Laravle包提供了一种代码生成器,使得你能够加速你的开发进程.这些生成器包含: generate:model – 模型生成器 generat ...
- 详述Centos中的ftp命令的使用方法
ftp服务器在网上较为常见,Linux ftp命令的功能是用命令的方式来控制在本地机和远程机之间传送文件,这里详细介绍Linux ftp命令的一些经常使用的命令,相信掌握了这些使用Linux 进行ft ...
- [Windows Server 2003] IIS自带FTP安装及配置方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:IIS6.0自 ...
- OpenJDK源码研究笔记(四)-编写和组织可复用的工具类和方法
本篇主要讲解java.util.Arrays这个针对数组的工具类. 1.可复用的工具类和方法. 这个工具类里,包含很多针对数组的工具方法,如 排序.交换.二分查找.比较.填充.复制.hashcode ...
随机推荐
- java并发之线程间通信
1.volatile 关键字 java 支持多个线程同时访问一个对象或对象的成员变量,而每个线程拥有这个变量的拷贝,虽然对象或成员变量分配的内存在共享内存,但每个执行的线程可以拥有一份拷贝,可以提高程 ...
- 20190402Linux常用命令week1.1
Linux常用命令详解week1.1 基础命令:lsmanpwdcdmkdirechotouchcpmvrmrmdircatmorelessheadtailclearpoweroffreboot 命令 ...
- 使用C#爬小说
最近因朋友需要在研究如何从网站上爬小说,说到爬,很多人首先想到的是Python,但是因为没有用过Python,加上时程比较紧,就直接使用C#. 其原理也很简单,就是利用HttpWebRequest对象 ...
- 机器学习【三】k-近邻(kNN)算法
一.kNN算法概述 kNN算法是用来分类的,其依据测量不同特征值之间的距离,其核心思想在于用距离目标最近的k个样本数据的分类来代表目标的分类(这k个样本数据和目标数据最为相似).其精度高,对异常值不敏 ...
- nginx配置优化 第二章
一:常用功能优化: 1:网络连接的优化: 只能在events模块设置,用于防止在同一一个时刻只有一个请求的情况下,出现多个睡眠进程会被唤醒但只能有一个进程可获得请求的尴尬,如果不优化,在多进程的ngi ...
- 类自动调用to.string方法
所有对象都有toString()这个方法,因为它是Object里面已经有了的方法,而所有类都是继承Object,所以“所有对象都有这个方法” 它通常只是为了方便输出,比如System.out.prin ...
- 【HNOI 2018】排列
Problem Description 给定 \(n\) 个整数 \(a_1, a_2, \ldots , a_n(0 \le a_i \le n)\),以及 \(n\) 个整数 \(w_1, w_2 ...
- docker从容器里面拷文件到宿主机或从宿主机拷文件到docker容器里面
1.从容器里面拷文件到宿主机? 答:在宿主机里面执行以下命令 docker cp 容器名:要拷贝的文件在容器里面的路径 要拷贝到宿主机的相应路径 示例: 假设容器名为testtomcat, ...
- JS开发工具WebStorm使用快捷键
快捷键可以提高开发效率,最好用的就是这些! 代码编辑 Ctrl + d 复制整行 Ctrl + '-/+' 模块折叠 Ctrl + [ ] 括号匹配 Ctrl + F12 结构展示 Shif ...
- _map_char_stats
可以控制玩家进入地图后进行属性平衡. `comment` 备注 `map` 地图ID `vip`vip等级 `shengming`生命 `liliang` 力量 `minjie` 敏捷 `zhili` ...