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

相关文章

phpmailer在服务器上不能正常发送邮件的解决办法

phpmailer本身是一个很不错的开源邮件类,也非常的易用简单,就是偶尔会出现程序上传到服务器上不能发送邮件的情况,在之前也有同学问过我这个问题,当时的时候总是不以为然,今天终于让我碰...

python探索之BaseHTTPServer-实现Web服务器介绍

在Python探索之SocketServer详解中我们介绍了Python标准库中的SocketServer模块,了解了要实现网络通信服务,就要构建一个服务器类和请求处理类。同时,该模块还...

python脚本监控Tomcat服务器的方法

python脚本监控Tomcat服务器的方法

文章出处:https://blog.csdn.net/sdksdk0/article/details/80933444 作者:朱培    &nb...

在 Django/Flask 开发服务器上使用 HTTPS

使用 Django 或 Flask 这种框架开发 web app 的时候一般都会用内建服务器开发和调试程序,等程序完成后再移交到生产环境部署。问题是这些内建服务器通常都不支持 HTTPS...

PHP使用SMTP邮件服务器发送邮件示例

PHP使用SMTP邮件服务器发送邮件示例

本文实例讲述了PHP使用SMTP邮件服务器发送邮件。分享给大家供大家参考,具体如下: 用之前记得先去163注册一个邮箱,然后打开SMTP服务,当然也可以使用QQ邮箱等,但配置信息得改。...