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

所属分类: 脚本专栏 / python 阅读数: 2092
收藏 0 赞 0 分享

复制代码 代码如下:

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 pands实现execl转csv 并修改csv指定列的方法

今天小编就为大家分享一篇python pands实现execl转csv 并修改csv指定列的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

对pandas读取中文unicode的csv和添加行标题的方法详解

今天小编就为大家分享一篇对pandas读取中文unicode的csv和添加行标题的方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python3实现多线程聊天室

这篇文章主要为大家详细介绍了python3实现多线程聊天室,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

解决sublime+python3无法输出中文的问题

今天小编就为大家分享一篇解决sublime+python3无法输出中文的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python判断一个数是否能被另一个整数整除的实例

今天小编就为大家分享一篇python判断一个数是否能被另一个整数整除的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

对Python 除法负数取商的取整方式详解

今天小编就为大家分享一篇对Python 除法负数取商的取整方式详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python矩阵的转置和逆转实例

今天小编就为大家分享一篇python矩阵的转置和逆转实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python将list转为matrix的方法

今天小编就为大家分享一篇python将list转为matrix的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

对Python的zip函数妙用,旋转矩阵详解

今天小编就为大家分享一篇对Python的zip函数妙用,旋转矩阵详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python reverse反转部分数组的实例

今天小编就为大家分享一篇python reverse反转部分数组的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多