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

Dior ·
更新时间:2024-09-20
· 571 次阅读

代码如下:
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

您可能感兴趣的文章:Python获取远程文件大小的函数代码分享python实现带错误处理功能的远程文件读取方法python通过paramiko复制远程文件及文件目录到本地python定时复制远程文件夹中所有文件python使用Paramiko模块实现远程文件拷贝Python通过paramiko远程下载Linux服务器上的文件实例python实现下载文件的三种方法Python实现HTTP协议下的文件下载方法总结Python实现批量下载文件Python实现的远程文件自动打包并下载功能示例



远程服务器 服务器下载 服务器 程序 日志文件 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章