发布需要用flask写了一个简陋的文件上传分发工具
功能手动上传文件,发送到指定服务器的目标目录
环境 python2+flask
目录结构
# coding:utf-8
from flask import Flask, render_template,request,redirect,url_for
from werkzeug.utils import secure_filename
import os,commands
app = Flask(__name__)
sever_ip = ("192.168.1.10",
"192.168.1.11",
"192.168.1.12")
@app.route('/', methods=['POST', 'GET'])
def upload():
if request.method == 'POST':
if 'upload' in request.form:
f = request.files['file']
up_file = open('/root/dalu/upload/tmp.cfg', 'w')
up_file.write(f.filename)
up_file.close()
print f.filename
basepath = os.path.dirname(__file__) # 当前文件所在路径
upload_path = os.path.join(basepath, './tmp/',secure_filename(f.filename)) #注意:没有的文件夹一定要先创建,不然会提示没有该路径
f.save(upload_path)
return render_template('upload.html')
else:
up_file = open('/root/dalu/upload/tmp.cfg', 'r+')
file = request.form.get('file', type=str, default=None)
local_file = up_file.read()
for sever in sever_ip:
publish = "ssh %s '\cp /data/www/%s /root/dalu/tmp/' && rsync -avzh '-e ssh -p 36001'" \
" ./tmp/%s root@%s:/data/www/%s" % (sever, file, local_file, sever, file)
os.system(publish)
(status,dir) = commands.getstatusoutput("ssh %s 'ls -l /data/www/%s'" % (sever, file))
cat = str(dir.strip('\n').split('\n'))
os.system(cat)
up_file.seek(0)
up_file.truncate()
up_file.close()
return render_template('upload.html', result=cat, ip=file)
return render_template('upload.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8888, debug=True)
upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>特殊文件发布</title>
</head>
<body>
<h1>1.文件上传</h1>
<form action="" enctype='multipart/form-data' method='POST'>
<input type="file" name="file">
<input type="submit"name="upload" value="上传">
</form>
<div>
<div >
<h1>2发布文件</h1>
<form id="" method="POST" action="">
<div>
<span>文件</span>
<input size="50" name="file" type="text"placeholder="ps:base/***/**.php" value="{{ file }}">
<span>
<input name="perform"type="submit" value="发布" />
</span>
</div>
</form>
</div>
<div id="result">
<h1>3发布结果</h1><span>{% if result %}{{ result }}{% endif %}</span>
</div>
</div>
</body>
</html>
···
···