python的ftp功能程序
ftp类模块:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ftplib import FTP
import sys
import datetime
import os.path
import ConfigParser
time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
class Ftp:
def __init__ (self,IP,USERNAME,PASSWD,URL,FILENAME):
"""初始化ip和端口"""
self.IP=IP
self.USERNAME=USERNAME
self.PASSWD=PASSWD
self.URL=URL
self.FILENAME=FILENAME
def ftp_down(self):
try:
ftp=FTP(self.IP)
except Exception:
print "连接服务器失败"
try:
ftp.login(self.USERNAME,self.PASSWD)
except Exception:
print "登陆服务器失败"
ftp.cwd(self.URL)
ftp.voidcmd("TYPE I")
if len(ftp.nlst(self.URL+os.sep+self.FILENAME)) != 0:
Datasock,estsize = ftp.ntransfercmd("RETR %s" % self.FILENAME )
Transbytes=0
FO=open(self.FILENAME,'wb')
while True:
buf=Datasock.recv(2048)
if not len(buf):
break
FO.write(buf)
Transbytes+=len(buf)
sys.stdout.write("Received %d" %Transbytes)
if estsize:
sys.stdout.write("of %d bytes (%.2f%%)\r\n" % \
(estsize,100.0 * float(Transbytes) / float(estsize)))
else:
sys.stdout.write("bytes\r")
sys.stdout.flush()
sys.stdout.write("bytes\r")
FO.close()
Datasock.close()
ftp.voidresp()
ftp.quit()
else:
print "下载文件不存在!--%s" % self.FILENAME
def ftp_up(self):
"""上传"""
try:
ftp=FTP(self.IP)
except Exception:
print "连接服务器失败"
try:
ftp.login(self.USERNAME,self.PASSWD)
except Exception:
print "登陆服务器失败"
ftp.cwd(self.URL)
ftp.voidcmd("TYPE I")
if os.path.isfile(self.FILENAME):
FO=open(self.FILENAME,'rb')
Datasock,esize=ftp.ntransfercmd('STOR %s' % self.FILENAME )
esize=os.stat(self.FILENAME)[6]
Transbytes = 0
while True:
buf = FO.read(2048)
if not len(buf):
break
Datasock.sendall(buf)
Transbytes+=len(buf)
sys.stdout.write("send %d of %d bytes (%.1f%%)\r" \
% ( Transbytes,esize,100 * float(Transbytes) / float(esize)))
sys.stdout.flush()
Datasock.close()
sys.stdout.write("\n")
FO.close()
ftp.voidresp()
ftp.quit()
else:
print "上传文件不存在!--%s" % self.FILENAME
class Config:
def DOWN(self):
config=ConfigParser.ConfigParser()
config_file=open('config.ini','r')
config.readfp(config_file)
ip=config.get('FTP_APP_DOWN','ip')
user=config.get('FTP_APP_DOWN','user')
passWord=config.get('FTP_APP_DOWN','password')
url=config.get('FTP_APP_DOWN','url')
filename=config.get('FTP_APP_DOWN','filename')
config_file.close()
down=Ftp(ip,user,password,url,filename)
down.ftp_down()
def UP(self):
config=ConfigParser.ConfigParser()
config_file=open('config.ini','r')
config.readfp(config_file)
ip=config.get('FTP_APP_UP','ip')
user=config.get('FTP_APP_UP','user')
password=config.get('FTP_APP_UP','password')
url=config.get('FTP_APP_UP','url')
filename=config.get('FTP_APP_UP','filename')
config_file.close()
up=Ftp(ip,user,password,url,filename)
up.ftp_up()
if __name__ == '__main__':
sys.stdout=open('ftp.log','a')
print '开始下载:%s' % time
FTP_2=Config()
FTP_2.DOWN()
print '下载结束:%s' % time
print ''
print '开始上传:%s' % time
FTP_2.UP()
print '上传结束:%s' % time
print ''
sys.stdout.close()
读取的配置文件config.ini:
[FTP_APP_DOWN]
ip=192.168.10.1
user=RHL
password=RHL4321
url=/usr/mpsp/yunxing/RHL
filename=ABC.txt
[FTP_APP_UP]
ip=192.168.10.2
user=RHL
password=RHL4321
url=/usr/mpsp/yunxing
filename=ABC.txt
相关文章