python 從遠程服務(wù)器下載日志文件的程序
import os
import sys
import ftplib
import socket
##################################################################
# sign in the ftp server and download the log file.
# 登陸生產(chǎn)服務(wù)器下載日志
#################################################################
def getServerLog(dir,fileName,host,userName,password):
if os.path.exists(fileName):
print '****the file '+ fileName +' has already exist! The file will be over writed'
#connect
try:
f=ftplib.FTP(host)
except (socket.error,socket.gaierror),e:
print '----ERROR:cannot reach '+host
print e
return False
#login
try:
f.login(user=userName,passwd=password)
except ftplib.error_perm ,e:
print '----ERROR:cannot login to server '+host
print e
f.quit()
return False
print '****Logged in as ' + userName + ' to server ' +host
#change folder
try:
f.cwd(dir)
except ftplib.error_perm,e:
print '----ERROR:cannot CD to %s on %s' % (dir,host)
print e
f.quit()
return False
print '**** changed to %s folder on %s' % (dir,host)
#get file
try:
f.retrbinary('RETR %s' % fileName,open(fileName,'wb').write)
except ftplib.error_perm,e:
print '----ERROR:cannot read file %s on %s' % (fileName,host)
print e
os.unlink(fileName)
return False
else:
print '****Downloaded '+ fileName +' from '+ host +' to '+os.getcwd()
f.quit()
return True
if __name__ == "__main__":
getServerLog("/userhome/root/other/temp","a.out","10.10.10.10","root","password")
print '****done'
運行:python getServerLog.py
相關(guān)文章
Python利用帶權(quán)重隨機數(shù)解決抽獎和游戲爆裝備問題
帶權(quán)重隨機數(shù)即是隨機數(shù)各個區(qū)間段被抽中的概率根據(jù)權(quán)重而不同,這里我們就來看一下Python利用帶權(quán)重隨機數(shù)解決抽獎和游戲爆裝備問題的方法,首先還是來進一步解釋帶權(quán)隨機數(shù):2016-06-06
python腳本生成caffe train_list.txt的方法
下面小編就為大家分享一篇python腳本生成caffe train_list.txt的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python調(diào)用機器喇叭發(fā)出蜂鳴聲(Beep)的方法
這篇文章主要介紹了python調(diào)用機器喇叭發(fā)出蜂鳴聲(Beep)的方法,實例分析了Python調(diào)用winsound模塊的使用技巧,需要的朋友可以參考下2015-03-03
Python實現(xiàn)字符串格式化的方法小結(jié)
本篇文章主要介紹了Python實現(xiàn)字符串格式化的方法小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Python中使用多進程來實現(xiàn)并行處理的方法小結(jié)
本篇文章主要介紹了Python中使用多進程來實現(xiàn)并行處理的方法小結(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08

