当前位置: 移动技术网 > IT编程>脚本编程>Python > python 从远程服务器下载日志文件的程序

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

2019年04月04日  | 移动技术网IT编程  | 我要评论

客家足迹行,高清蓝光,条码识别器

复制代码 代码如下:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网