使用到的模块 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下操作:需要【连接,输用户名,输密码,单文件操作,存在超时限制】 太过于繁琐,容易操作失败 改进一句命令,搞定多文件上传,下载,查询,列表等操作 后期可以加入更强大的功能 源代码
|
下一篇:PYTHON 源码阅读 – 对象