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


Posted in Python onFebruary 10, 2013
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 相关文章推荐
python判断一个集合是否包含了另外一个集合中所有项的方法
Jun 30 Python
动感网页相册 python编写简单文件夹内图片浏览工具
Aug 17 Python
python3读取MySQL-Front的MYSQL密码
May 03 Python
Python 列表理解及使用方法
Oct 27 Python
Tensorflow卷积神经网络实例
May 24 Python
Python解决线性代数问题之矩阵的初等变换方法
Dec 12 Python
对python修改xml文件的节点值方法详解
Dec 24 Python
Python3.4学习笔记之 idle 清屏扩展插件用法分析
Mar 01 Python
Python中请不要再用re.compile了
Jun 30 Python
Python 硬币兑换问题
Jul 29 Python
python和php哪个更适合写爬虫
Jun 22 Python
Python+Appium新手教程
Apr 17 Python
使用python检测手机QQ在线状态的脚本代码
Feb 10 #Python
通过python下载FTP上的文件夹的实现代码
Feb 10 #Python
打开电脑上的QQ的python代码
Feb 10 #Python
使用python实现baidu hi自动登录的代码
Feb 10 #Python
python判断端口是否打开的实现代码
Feb 10 #Python
python登录QQ邮箱发信的实现代码
Feb 10 #Python
可用于监控 mysql Master Slave 状态的python代码
Feb 10 #Python
You might like
PHPAnalysis中文分词类详解
2014/06/13 PHP
yii2.0整合阿里云oss的示例代码
2017/09/19 PHP
js arguments.callee的应用代码
2009/05/07 Javascript
jQuery EasyUI API 中文文档 - NumberBox数字框
2011/10/13 Javascript
jQuery在html有效在jsp无效的原因及解决方法
2013/08/02 Javascript
在javaScript中关于submit和button的区别介绍
2013/10/20 Javascript
Mac/Windows下如何安装Node.js
2013/11/22 Javascript
《JavaScript DOM 编程艺术》读书笔记之DOM基础
2015/01/09 Javascript
js判断登录与否并确定跳转页面的方法
2015/01/30 Javascript
AngularJS入门(用ng-repeat指令实现循环输出
2016/05/05 Javascript
JS获取本周周一,周末及获取任意时间的周一周末功能示例
2017/02/09 Javascript
微信小程序多列选择器range-key使用详解
2020/03/30 Javascript
JS实现的透明度渐变动画效果示例
2018/04/28 Javascript
centos 上快速搭建ghost博客方法分享
2018/05/23 Javascript
解决vuejs项目里css引用背景图片不能显示的问题
2018/09/13 Javascript
JavaScript两种计时器的实例讲解
2019/01/31 Javascript
vue滚动tab跟随切换效果
2020/06/29 Javascript
javascript实现获取中文汉字拼音首字母
2020/05/19 Javascript
JS实现超级好看的鼠标小尾巴特效
2020/12/01 Javascript
深入解析Python设计模式编程中建造者模式的使用
2016/03/02 Python
python文件写入write()的操作
2019/05/14 Python
Django框架封装外部函数示例
2019/05/28 Python
爬虫代理池Python3WebSpider源代码测试过程解析
2019/12/20 Python
data:image data url 文件转为Blob上传后端的方法
2019/07/16 HTML / CSS
美国当红的名品折扣网:Gilt Groupe
2016/08/15 全球购物
经济学人订阅:The Economist
2018/07/19 全球购物
优秀医生事迹材料
2014/02/12 职场文书
人力资源主管的岗位职责
2014/03/15 职场文书
社区道德讲堂实施方案
2014/03/21 职场文书
消防宣传口号
2014/06/16 职场文书
电教室标语
2014/06/20 职场文书
财务助理岗位职责范本
2014/10/09 职场文书
学校师德师风整改措施
2014/10/27 职场文书
2015年教学副校长工作总结
2015/07/22 职场文书
python 如何获取页面所有a标签下href的值
2021/05/06 Python
看完这篇文章获得一些java if优化技巧
2021/07/15 Java/Android