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

yipeiwu_com6年前服务器

复制代码 代码如下:

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通过邮件服务器端口发送邮件的方法。分享给大家供大家参考。具体实现方法如下: fromAddress = 'sender@example.com' toAdd...

如何使用php判断服务器是否是HTTPS连接

复制代码 代码如下:if ($_SERVER['HTTPS'] != "on") { echo "This is not HTTPS";}else{ echo "Th...

django自带调试服务器的使用详解

django自带调试服务器的使用详解

开启服务器 在终端(虚拟环境)下输入: python manage.py runserver 就可以开启服务器 输入后,注意随后弹出的服务器地址。 点击后就会跳转至调试服务器。...

【不二】关于affiliate和niche站运营的6条建议

【不二】关于affiliate和niche站运营的6条建议

今天圈友@不二  来给大家分享一些affiliate和英文niche站的的入门玩法,给新手做个科普。 niche,中文一般翻译为利基,可以理解成长尾细分市场,这些细分领域一般来说,市场...

浅析虚拟主机服务器php fsockopen函数被禁用的解决办法

一、如何禁用fsockopen()下面是两种常用的禁用fsockopen的方法。1、修改php.ini,将 disable_functions = 后加入 fsockopen 2、修改p...