加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python 从远程服务器下载日志文件的程序

发布时间:2020-12-16 20:06:16 所属栏目:Python 来源:网络整理
导读:复制代码 代码如下: import os import sys import ftplib import socket ################################################################## # sign in the ftp server and download the log file. # 登陆生产服务器下载日志 ###########################

复制代码 代码如下:

import os
import sys
import ftplib
import socket

##################################################################
# sign in the ftp server and download the log file.
# 登陆生产服务器下载日志
#################################################################
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读