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实现telnet服务器的方法

本文实例讲述了Python实现telnet服务器的方法。分享给大家供大家参考。具体实现方法如下: import threading class myThread(threading...

php简单socket服务器客户端代码实例

本篇文章分享一个简单的socket示例,用php。实现一个接收输入字符串,处理并返回这个字符串到客户端的TCP服务。 产生一个 socket 服务端 <?php /*文...

Python 搭建Web站点之Web服务器网关接口

Python 搭建Web站点之Web服务器网关接口

在 Python 搭建Web站点之Web服务器与Web框架 中我们弄清楚了Web 服务器、Web 应用程序、Web框架的概念。对于 Python 来说,越来越多的 Web 框架面世,在给...

php在服务器执行exec命令失败的解决方法

php在服务器执行exec命令失败的解决方法

前言:本文针对windows php环境,linux系统不在讨论范畴。       出于安全的原因,服务器是不允许php或者其他语言执...

Ruby使用eventmachine为HTTP服务器添加文件下载功能

思路: 使用ruby eventmachine和em-http-server gem,完成一个简单的提供文件下载功能的HttpServer; 使用了EM的FileStreamer来异步发...