python 基于windows环境的ftp功能
描述:
1、基于备份服务器部署的py程序,将需要备份主机目录下的内容下载至备份服务器(服务端和远端都是windows server 2008)
2、py程序部署在windows服务器,后台运行,基于bat脚本启停程序
Windows server 2008 FTP环境配置
1、安装FTP服务
开始 --》管理工具 --》服务器管理器
2、安装IIS/FTP角色
打开服务器管理器,找到添加角色,然后点击,弹出添加角色对话框,选择下一步
下一步
选择Web服务器(IIS),然后选择FTP服务,直到安装完成。
参考:http://www.cnblogs.com/Denny_Yang/p/3741041.html
FTP代码
class Windows_ftp(object):
'''
FTP类,基于ftplib模块实现
connect: 连接
login: 登陆
DownLoadFile: 下载文件
DownLoadFileTree: 下载指定目录下的所有文件和目录
UpLoadFile: 上传文件
UpLoadFileTree: 上传指定目录下的所有文件和目录
isDir: 判断是否为目录
cwd: 变更目录
quit: 退出
'''
ftp = ftplib.FTP()
bIsDir = False
path = "" def __init__(self, host, port, username, password):
self.host = host
self.port = port
self.username = username
self.password = password
self.buffer = 4096 # 设置的缓冲区大小
self.ftp.set_debuglevel(0) # 打开调试级别2,显示详细信息; 级别0关闭调试模式 def connect(self):
try:
self.ftp.connect(self.host, self.port, timeout=10)
logger.info('*** connected to host "%s"' % self.host)
return True
except Exception as err:
logger.error('cannot reach "%s", %s' % (self.host, err))
return False def login(self):
try:
self.ftp.login(self.username, self.password)
logger.info('*** Login successfully "%s"' % self.username)
return True
except Exception as err:
logger.error('cannot login, %s' % err)
self.quit()
return False def DownLoadFile(self, LocalFile, RemoteFile):
file_handler = open(LocalFile, 'wb')
self.ftp.retrbinary("RETR %s" % RemoteFile, file_handler.write, self.buffer)
file_handler.close()
return True def show(self, list):
result = list.split(" ")
#logger.debug(result)
if self.path in result and "<DIR>" in result:
self.bIsDir = True def isDir(self, path):
self.bIsDir = False
self.path = path
#this ues callback function ,that will change bIsDir value
self.ftp.retrlines('LIST', self.show)
return self.bIsDir def DownLoadFileTree(self, LocalDir, RemoteDir):
if os.path.isdir(LocalDir) == False: # 判断本地主机是否存在目录,进行创建
os.makedirs(LocalDir)
try:
self.cwd(RemoteDir)
except Exception as err:
logger.error("Failed to open the path to the remote host, %s" % err)
return False
RemoteNames = self.ftp.nlst() # 列出远程下载目录下所有内容
logger.debug("Remote downLoad path:%s, DownLoad files list:%s" % (RemoteDir, RemoteNames))
for file_or_path in RemoteNames:
Local = os.path.join(LocalDir, file_or_path)
if self.isDir(file_or_path):
self.DownLoadFileTree(Local, file_or_path)
else:
self.DownLoadFile(Local, file_or_path)
self.ftp.cwd("..")
return def cwd(self, DIRN):
try:
self.ftp.cwd(DIRN)
except Exception as err:
logger.error('ERROR:cannot CD to "%s"' % DIRN)
logger.error(err)
self.quit()
logger.debug('*** changed to "%s" folder' % DIRN) def UpLoadFile(self, LocalFile, RemoteFile):
if os.path.isfile(LocalFile) == False:
return False
file_handler = open(LocalFile, "rb")
self.ftp.storbinary('STOR %s' % RemoteFile, file_handler, self.buffer)
file_handler.close()
return True def UpLoadFileTree(self, LocalDir, RemoteDir):
if os.path.isdir(LocalDir) == False:
return False
LocalNames = os.listdir(LocalDir)
logger.debug("Remote upLoad path:%s, UpLoad files list:%s" % (RemoteDir, LocalDir))
self.cwd(RemoteDir)
for Local in LocalNames:
src = os.path.join(LocalDir, Local)
if os.path.isdir(src):
self.UpLoadFileTree(src, Local)
else:
self.UpLoadFile(src, Local)
self.ftp.cwd("..")
return def quit(self):
self.ftp.quit()
参考:http://www.sharejs.com/codes/python/5619
windows脚本
start.bat
@echo off
if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Running.."
ping -n 3 localhost >nul
exit
)else ( echo "[%date% %time%] Starting.."
start pythonw hk_win_syncfile.py
ping -n 3 localhost >nul
status.bat
ping -n 3 localhost >nul
)
stop.bat
@echo off if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Stopping..."
python -c "import os; os.system('taskkill /F /PID %%s' %% open('./var/hk_win_syncfile.pid').read());"
del /s hk_win_syncfile.pid
ping -n 3 localhost >nul
)else (echo "[%date% %time%] Stopped.."
ping -n 3 localhost >nul
)
status.bat
@echo off
if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Runningg...")else (echo "[%date% %time%] Stopped..")
ping -n 3 localhost >nul
restart.bat
@echo off
if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Stopping..."
python -c "import os; os.system('taskkill /F /PID %%s' %% open('./var/hk_win_syncfile.pid').read());"
del /s hk_win_syncfile.pid
ping -n 3 localhost >nul
)else (echo "[%date% %time%] Stopped.."
ping -n 3 localhost >nul
) if exist ./var/hk_win_syncfile.pid (echo "[%date% %time%] Running.."
ping -n 3 localhost >nul
exit
)else ( echo "[%date% %time%] Starting.."
start pythonw hk_win_syncfile.py
ping -n 3 localhost >nul
status.bat
ping -n 3 localhost >nul
)
参考:http://my.oschina.net/sanpeterguo/blog/337263
python 基于windows环境的ftp功能的更多相关文章
- 基于Windows环境下cmd/编译器无法输入中文,显示中文乱码解决方案
基于Windows环境下cmd/编译器无法输入中文,显示中文乱码解决方案 两个月前做C++课设的时候,电脑编译器编译结果出现了中文乱码,寻求了百度和大神们,都没有解决这个问题,百度上一堆解释是对编译器 ...
- 基于windows环境VsCode的ESP32开发环境搭建
1. 基于windows环境VsCode的ESP32开发环境搭建,网上有各类教程,但是我实测却不行. 例如我在vscode内安装的乐鑫插件,扩展配置项是下图这样: 而百度的各类博文却都是这样: 经过网 ...
- 【Kafka】基于Windows环境的Kafka有关环境(scala+zookeeper+kafka+可视化工具)搭建、以及使用.NET环境开发的案例代码与演示
前言:基于Windows系统下的Kafka环境搭建:以及使用.NET 6环境进行开发简单的生产者与消费者的演示. 一.环境部署 Kafka是使用Java语言和Scala语言开发的,所以需要有对应的Ja ...
- python添加Windows环境变量
1.cmd中添加方式 SET PATH=%PATH%;c:\Program Files (x86)\Wireshark 注:如上代码添加c:\Program Files (x86)\Wireshark ...
- python在windows环境安装MySQLdb
一.环境 系统:win7,64位 python版本:2.7.15 pip版本:10.0.1 二.安装 1. 用pip安装 pip install MySQLdb 报错: Could not find ...
- 基于windows环境的Flask网站搭建(mysql + conda + redis)
1下载mysql-installer-community-5.7.24.0.msi (https://dev.mysql.com/downloads/windows/installer/8.0.htm ...
- python网络编程--socketserver 和 ftp功能简单说明
1. socketserver 我们之前写的tcp协议的socket是不是一次只能和一个客户端通信,如果用socketserver可以实现和多个客户端通信.它是在socket的基础上进行了一层封装,也 ...
- 基于Windows环境下Myeclipse10.0下载安装破解及jdk的下载安装及环境变量的配置
jdk的安装及环境变量的配置 1.安装JDK开发环境 附上jdk安装包的百度云链接 链接:http://pan.baidu.com/s/1mh6QTs8 密码:jkb6(当然自行去官网下载最好哒,可以 ...
- 让你用sublime写出最完美的python代码--windows环境
至少很长一段时间内,我个人用的一直是pycharm,也感觉挺好用的,也没啥大毛病 但是pycharm确实有点笨重,啥功能都有,但是有很多可能这辈子我也不会用到,并且pycharm打开的速度确实不敢恭维 ...
随机推荐
- .NET 平台下的插件化开发内核(Rabbit Kernel)
每个程序猿都有一个框架梦,曾经在2013年8月15日写过一篇"Koala Framework是什么?我为什么要写这个框架?"的文章,在开放框架路上迈出了第一步,之后作者如愿找到了一 ...
- python2.7到python3代码转换脚本2to3的一些介绍
你的位置: Home ‣ Dive Into Python 3 ‣ 难度等级: ♦♦♦♦♦ 使用2to3将代码移植到Python 3 ❝ Life is pleasant. Death is ...
- ListView适配器获取布局文件作为View的三种方式
第一种方法: public View getView(int position, View convertView, ViewGroup parent) { View view = null; if ...
- canvas三角函数应用
这个是圆圈旋转的简单案例 var canvas=document.getElementById("canvas"); var cxt=canvas.getContext(" ...
- 一个简单的网页布局HTML+CSS
<!doctype html> <html> <head> <meta charset="utf-8"/> <title> ...
- HttpModule与HttpHandler详解
ASP.NET对请求处理的过程:当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给 ASPNET_ISAPI.dll,A ...
- [转]响应式WEB设计学习(1)—判断屏幕尺寸及百分比的使用
原文地址:http://www.jb51.net/web/70360.html 现在移动设备越来越普及,用户使用智能手机.pad上网页越来越普遍.但是传统的fix型的页面在移动终端上无法很好的显示.因 ...
- JAVA 一个或多个空格分割字符串
知识补充 String的split方法支持正则表达式: 正则表达式\s表示匹配任何空白字符,+表示匹配一次或多次. 有了以上补充知识,下面的内容就很好理解了. 一.待分割字符串 待分割字符串为如下: ...
- 使用oracle utl_http包需要注意的事项
总结下几次使用utl_http包遇到的几个问题 关于utl_http包功能还是很强大的 可以通过他来捕捉网站页面的内容 或者调用一个url的接口完成某项功能 Eg: declare req UT ...
- 【poj2699】 The Maximum Number of Strong Kings
http://poj.org/problem?id=2699 (题目链接) 题意 给出1张有向完全图.U->V表示U可以打败V并得一分.如果一个人的得分最高,或者他打败所有比自己得分高的人,那么 ...