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

yipeiwu_com6年前服务器

复制代码 代码如下:

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

相关文章

同台服务器使用缓存APC效率高于Memcached的演示代码

复制代码 代码如下:<?php $memcachehost = 'localhost'; $memcacheport = '11211'; function microtime_f...

Ubuntu下Python+Flask分分钟搭建自己的服务器教程

Ubuntu下Python+Flask分分钟搭建自己的服务器教程

最近帮朋友做了点东西,最后需要将结果在网页中展示,这就需要搭建个服务器,做几个网页把数据信息展示出来。网上找了一下,阿里腾讯都有租服务器的业务,但是有的时候我们并不需要那么复杂大型的服务...

Python XML RPC服务器端和客户端实例

Python XML RPC服务器端和客户端实例

一、远程过程调用RPC XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a tra...

python实现linux服务器批量修改密码并生成execl

批量修改linux服务器密码,同时生成execl表格 复制代码 代码如下:#!/usr/bin/env python#coding:utf8#随机生成自定义长度密码from random...

Python实现简易版的Web服务器(推荐)

Python实现简易版的Web服务器(推荐)

下面给大家介绍python实现简易版的web服务器,具体内容详情大家通过本文学习吧! 1、请自行了解HTTP协议 /post/133883.htm(点击跳转) 2、创建Socket服务...