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

yipeiwu_com4年前服务器

复制代码 代码如下:

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

相关文章

配置.htaccess文件 使多个域名指向同一空间下的不同目录 一个空间多个域名绑定

创建《.htaccess文件》,配置一级域名指向Web根目录的子目录。<IfModule mod_rewrite.c> Options +FollowSymlink...

PHP 显示客户端IP与服务器IP的代码

来看看代码: 复制代码 代码如下: echo "(1)浏览当前页面的用户的 IP 地址为:"; echo $_SERVER['REMOTE_ADDR']; echo "<br /&...

使用Python来编写HTTP服务器的超级指南

使用Python来编写HTTP服务器的超级指南

首先,到底什么是网络服务器? 简而言之,它是在物理服务器上搭建的一个网络连接服务器(networking server),永久地等待客户端发送请求。当服务器收到请求之后,它会生成响应...

python实现静态服务器

本文实例为大家分享了python静态服务器的具体代码,供大家参考,具体内容如下 #coding:utf-8 import socket import multiprocessing...

在IIS服务器上以CGI方式运行Python脚本的教程

在IIS服务器上以CGI方式运行Python脚本的教程

由于接触到Python Web开发,正好把最简单的CGI方式研究了一下,话说在Windows下配置Python的Web开发还真的蛮麻烦的,Linux下配置倒挺容易,正好微软有技术文章《U...