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

相关文章

PHP服务器页面间跳转实现方法

(注意不是用 header ,js 等方法做的客户端跳转) 复制代码 代码如下: function server_transfer($dest) { global ...; // 把希望...

PHP 高并发和大流量框架解决方案

一、高并发的概念在互联网时代,并发,高并发通常是指并发访问。也就是在某个时间点,有多少个访问同时到来。二、高并发架构相关概念1、QPS (每秒查询率) : 每秒钟请求或者查询的数量,在互联网领域,指每...

开启CURL扩展,让服务器支持PHP curl函数(远程采集)

curl()、file_get_contents()、snoopy.class.php这三个远程页面抓取或采集中用到的工具,默迹还是侵向于用snoopy.class.php,因为他效率比...

Python实现的FTP通信客户端与服务器端功能示例

Python实现的FTP通信客户端与服务器端功能示例

本文实例讲述了Python实现的FTP通信客户端与服务器端功能。分享给大家供大家参考,具体如下: 一 代码 1、服务端代码 import socket import threadin...

100行PHP代码实现socks5代理服务器

前两天在B站上看到一个小伙纸100元组装个电脑打LOL画质流畅,突发奇想100行代码能(简单)实现个啥好玩的。我主要是做php开发的,于是就有了本文。 当然,由于php(不算swoole...