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进行稳定可靠的文件操作详解
Dec 31 Python
python正常时间和unix时间戳相互转换的方法
Apr 23 Python
python中__slots__用法实例
Jun 04 Python
使用Python设计一个代码统计工具
Apr 04 Python
flask应用部署到服务器的方法
Jul 12 Python
python django 原生sql 获取数据的例子
Aug 14 Python
python打印n位数“水仙花数”(实例代码)
Dec 25 Python
Python hashlib常见摘要算法详解
Jan 13 Python
Python字典深浅拷贝与循环方式方法详解
Feb 09 Python
Anaconda的安装及其环境变量的配置详解
Apr 22 Python
Python能做什么
Jun 02 Python
Flask中sqlalchemy模块的实例用法
Aug 02 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
Apache设置虚拟WEB
2006/10/09 PHP
phpmyadmin操作流程
2006/10/09 PHP
超级好用的一个php上传图片类(随机名,缩略图,加水印)
2010/06/30 PHP
php 文件缓存函数
2011/10/08 PHP
Destoon旺旺无法正常显示,点击提示“会员名不存在”的解决办法
2014/06/21 PHP
PHP使用递归按层级查找数据的方法
2019/11/10 PHP
LBS blog sql注射漏洞[All version]-官方已有补丁
2007/08/26 Javascript
简单通用的JS滑动门代码
2008/12/19 Javascript
JavaScript 应用技巧集合[推荐]
2009/08/30 Javascript
SOSO地图API使用(一)在地图上画圆实现思路与代码
2013/01/15 Javascript
js 得到文件后缀(通过正则实现)
2013/07/08 Javascript
js实现日期级联效果
2014/01/23 Javascript
javascript事件函数中获得事件源的两种不错方法
2014/03/17 Javascript
JavaScript使用FileSystemObject对象写入文本文件内容的方法
2015/08/05 Javascript
Java框架SSH结合Easyui控件实现省市县三级联动示例解析
2016/06/12 Javascript
JS实现类似百叶窗下拉菜单效果
2016/12/30 Javascript
Linux Centos7.2下安装nodejs&npm配置全局路径的教程
2018/05/15 NodeJs
nodejs aes 加解密实例
2018/10/10 NodeJs
解决vue v-for src 图片路径问题 404
2019/11/12 Javascript
Angular8 实现table表格表头固定效果
2020/01/03 Javascript
Javascript实现贪吃蛇小游戏(含详细注释)
2020/10/23 Javascript
Django日志模块logging的配置详解
2017/02/14 Python
浅谈Python Opencv中gamma变换的使用详解
2018/04/02 Python
用python 实现在不确定行数情况下多行输入方法
2019/01/28 Python
不归路系列:Python入门之旅-一定要注意缩进!!!(推荐)
2019/04/16 Python
利用Tensorflow的队列多线程读取数据方式
2020/02/05 Python
python3 deque 双向队列创建与使用方法分析
2020/03/24 Python
Python try except else使用详解
2021/01/12 Python
python 求两个向量的顺时针夹角操作
2021/03/04 Python
CSS3实现网站商品展示效果图
2020/01/18 HTML / CSS
Mio Skincare法国官网:身体紧致及孕期身体护理
2018/04/04 全球购物
鞋类设计与工艺专业销售求职信
2013/11/01 职场文书
数控技校生自我鉴定
2014/04/19 职场文书
金融系应届毕业生求职信
2014/05/26 职场文书
2016年党支部公开承诺书
2016/03/25 职场文书
SQL Server连接查询的实用教程
2021/04/07 SQL Server