flask-文件上传分发工具

发布需要用flask写了一个简陋的文件上传分发工具
功能手动上传文件,发送到指定服务器的目标目录
环境 python2+flask
目录结构

在这里插入图片描述

Sample.py

# 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>

···

···
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值