网站建设资讯

NEWS

网站建设资讯

python脚本之ftp上传日志

因为ssoc日志巨大,很快就把磁盘占满。需要每天把备份上传到ftp服务器上,所以根据网上的资料,做了个简单的脚本。算是第一次自己拼凑出的脚本。还很简单,特别是把异常处理简化了。因为本身单一,然后把屏幕输出用管道命令》直接写到本地文件,充当日志。很懒的一个版本。还需加工。

创新互联公司是一家以成都网站建设、网页设计、品牌设计、软件运维、成都网站推广、小程序App开发等移动开发为一体互联网公司。已累计为广告制作等众行业中小客户提供优质的互联网建站和软件开发服务。


1 ftp上传 文件夹里的内容
2 上传后把现有的目录下的文件删除。
简化的好处就是只要遍历文件如果有新文件的就上传。

代码如下:


import ftplib
import os
import shutil
import time

def ftpconnect():
ftp_server = 'x.x.x.x' # FTP server ip address
username = 'xxxx'
password = 'xxxx'
timeout = 30
port = 21

ftp = ftplib.FTP()
ftp.set_debuglevel(2)  # open debug level 2, can display detail message
ftp.connect(ftp_server, port, timeout)  # connect to FTP server
ftp.login(username, password)

return ftp

def uploadfile_to_FTP():
ftp = ftpconnect()
print ftp.getwelcome() # can display FTP server welcome message.

bufsize = 1024
for filename in os.listdir(r"/data/data/event"):
    remotepath = "/safe-logs/"+filename
    localpath = "/data/data/event/"+filename
    fp = open(localpath, 'rb')
    ftp.storbinary('STOR ' + remotepath, fp, bufsize)  # start to upload file :local --> FTP server
ftp.set_debuglevel(0)  # close debug

fp.close()  # close connect

ftp.quit()  # quit FTP server

def cleanfile():
shutil.rmtree("/data/data/event")
os.mkdir("/data/data/event")

def print_time():
localtime=time.asctime(time.localtime(time.time()))
print '\n'
print "localtime:",localtime

if name== "main":

downloadfile_from_FTP()

print_time()
uploadfile_to_FTP()
cleanfile()


当前题目:python脚本之ftp上传日志
网站URL:http://cdweb.net/article/pscsdc.html