首页 存档 技术 查看内容

Python实现ftp常用操作[ftplib]

2018-3-30 13:00 |来自: 互联网 319 0

摘要: (点击上方公号,可快速关注) 来源:伯乐在线 / wklken 链接:http://python.jobbole.com/83446/ 使用到的模块 ftplib(http://docs.python.org/2/library/ftplib.html) 代码托管位置 github-pytools(https:// ...

(点击上方公号,可快速关注)


来源:伯乐在线 / wklken

链接:http://python.jobbole.com/83446/


使用到的模块 ftplib(http://docs.python.org/2/library/ftplib.html)

代码托管位置 github-pytools(https://github.com/wklken/pytools)

需求

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

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

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

改进

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

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

源代码

#!/usr/bin/python

# -*- coding:utf-8 -*-

#ftp.py

# [email protected]

#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.retrbinary("RETR %s"%filename, f, CONST_BUFFER_SIZE)

except ftplib.error_perm:

return False

return True

def list(ftp):

ftp.dir()

def find(ftp,filename):

ftp_f_list = ftp.nlst()

if filename in ftp_f_list:

return True

else:

return False

def help():

print("help info:")

print("[./ftp.py l]t show the file list of the ftp site ")

print("[./ftp.py f filenamA filenameB]t check if the file is in the ftp site")

print("[./ftp.py p filenameA filenameB]t upload file into ftp site")

print("[./ftp.py g filenameA filenameB]t get file from ftp site")

print("[./ftp.py h]t show help info")

print("other params are invalid")

def main():

args = sys.argv[1:]

if len(args) == 0:

print("Params needed!")

sys.exit(0)

ftp = connect()

success_list = []

failed_list = []

if args[0] == "p":

f_list = args[1:]

for up_file in f_list:

if not os.path.exists(up_file):

print(("UPLOAD: %s " COLOR_RED "FAILED" COLOR_NONE " :file not exist")%up_file)

continue

elif not os.path.isfile(up_file):

print(("UPLOAD: %s " COLOR_RED "FAILED" COLOR_NONE " :%s is not a file")%(up_file,up_file))

continue

if upload(ftp, up_file):

success_list.append(up_file)

else:

failed_list.append(up_file)

if len(success_list)

声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

相关分类

返回顶部