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

yipeiwu_com5年前服务器

复制代码 代码如下:

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探索之实现一个简单的HTTP服务器

Python标准库中的BaseHTTPServer模块实现了一个基础的HTTP服务器基类和HTTP请求处理类。这在文章python探索之BaseHTTPServer-实现Web服务器介绍...

PHP 实现文件分片上传的实例代码

PHP用超级全局变量数组$_FILES来记录文件上传相关信息的。file_uploads=on/off是否允许通过http方式上传文件max_execution_time=30允许脚本最大执行时间,超...

服务器变量 $_SERVER 的深入解析

服务器变量 $_SERVER 的深入解析: 1、$_SESSION['PHP_SELF'] -- 获取当前正在执行脚本的文件名 2、$_SERVER['SERVER_PROTOCOL']...

ffmpeg通过参数设置调整画质清晰度

ffmpeg通过参数设置调整画质清晰度

H.264有四种画质级别,分别是baseline, extended, main, high:1、Baseline Profile:基本画质。支持I/P 帧,只支持无交错(Progressive)和C...

深入Memcache的Session数据的多服务器共享详解

一相关介绍1.memcache + memcache的多服务器数据共享的介绍,请参见http://www.guigui8.com/index.php/archives/206.html2...