python腳本之ftp上傳日志

因?yàn)閟soc日志巨大,很快就把磁盤(pán)占滿。需要每天把備份上傳到ftp服務(wù)器上,所以根據(jù)網(wǎng)上的資料,做了個(gè)簡(jiǎn)單的腳本。算是第一次自己拼湊出的腳本。還很簡(jiǎn)單,特別是把異常處理簡(jiǎn)化了。因?yàn)楸旧韱我?,然后把屏幕輸出用管道命令》直接?xiě)到本地文件,充當(dāng)日志。很懶的一個(gè)版本。還需加工。

創(chuàng)新互聯(lián)公司是一家以成都網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、成都網(wǎng)站推廣、小程序App開(kāi)發(fā)等移動(dòng)開(kāi)發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為廣告制作等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開(kāi)發(fā)服務(wù)。


1 ftp上傳 文件夾里的內(nèi)容
2 上傳后把現(xiàn)有的目錄下的文件刪除。
簡(jiǎn)化的好處就是只要遍歷文件如果有新文件的就上傳。

代碼如下:


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()

網(wǎng)站題目:python腳本之ftp上傳日志
網(wǎng)站路徑:http://bm7419.com/article12/pscsdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、微信小程序App設(shè)計(jì)、外貿(mào)建站、定制網(wǎng)站、

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)