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循环监控远程端口的方法
Mar 14 Python
Python IDLE 错误:IDLE''s subprocess didn''t make connection 的解决方案
Feb 13 Python
Python 列表(List) 的三种遍历方法实例 详解
Apr 15 Python
python 遍历目录(包括子目录)下所有文件的实例
Jul 11 Python
对python的unittest架构公共参数token提取方法详解
Dec 17 Python
Python文件操作中进行字符串替换的方法(保存到新文件/当前文件)
Jun 28 Python
python3 使用Opencv打开USB摄像头,配置1080P分辨率的操作
Dec 11 Python
使用pyqt5 tablewidget 单元格设置正则表达式
Dec 13 Python
Python视频编辑库MoviePy的使用
Apr 01 Python
jupyter notebook读取/导出文件/图片实例
Apr 16 Python
python+requests接口压力测试500次,查看响应时间的实例
Apr 30 Python
解析Tensorflow之MNIST的使用
Jun 30 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
php Memcache 中实现消息队列
2009/11/24 PHP
PHP判断文章里是否有图片的简单方法
2014/07/26 PHP
Parse正式发布开源PHP SDK
2014/08/11 PHP
PHP 使用memcached简单示例分享
2015/03/05 PHP
PHP获取一年有几周以及每周开始日期和结束日期
2015/08/06 PHP
PHP迭代与递归实现无限级分类
2017/08/28 PHP
PHP设计模式之模板模式定义与用法详解
2018/12/20 PHP
用js实现随机返回数组的一个元素
2007/08/13 Javascript
再谈ie和firefox下的document.all属性
2009/10/21 Javascript
javascript 面向对象的JavaScript类
2010/05/04 Javascript
js监听输入框值的即时变化onpropertychange、oninput
2011/07/13 Javascript
js简单实现Select互换数据的方法
2015/08/17 Javascript
js与jQuery实现checkbox复选框全选/全不选的方法
2016/01/05 Javascript
Bootstrap页面布局基础知识全面解析
2016/06/13 Javascript
JavaScript运动框架 多物体任意值运动(三)
2017/05/17 Javascript
浅谈react.js中实现tab吸顶效果的问题
2017/09/06 Javascript
React实践之Tree组件的使用方法
2017/09/30 Javascript
vue实例中data使用return包裹的方法
2018/08/27 Javascript
Javascript数组方法reduce的妙用之处分享
2019/06/10 Javascript
在vue中利用全局路由钩子给url统一添加公共参数的例子
2019/11/01 Javascript
Jquery+javascript实现支付网页数字键盘
2020/12/21 jQuery
35个Python编程小技巧
2014/04/01 Python
Python xlrd读取excel日期类型的2种方法
2015/04/28 Python
Python多线程、异步+多进程爬虫实现代码
2016/02/17 Python
Python基于win32ui模块创建弹出式菜单示例
2018/05/09 Python
利用pyecharts读取csv并进行数据统计可视化的实现
2020/04/17 Python
欧舒丹美国官网:L’Occitane美国
2018/02/23 全球购物
运动会领导邀请函
2014/01/10 职场文书
巾帼文明岗申报材料
2014/05/01 职场文书
模特大赛策划方案
2014/05/28 职场文书
党委书记群众路线对照检查材料思想汇报
2014/10/04 职场文书
教师思想作风整顿个人剖析材料
2014/10/10 职场文书
上课说话检讨书
2015/01/27 职场文书
高中团支书竞选稿
2015/11/21 职场文书
Nginx+SpringBoot实现负载均衡的示例
2021/03/31 Servers
python对文档中元素删除,替换操作
2022/04/02 Python