• python文件服务器


    # -*- coding: UTF-8 -*-
    
    import os
    import sys
    import json
    import paramiko
    
    from flask import Flask,abort,request,render_template,send_from_directory
    from werkzeug.utils import secure_filename
    
    app = Flask(__name__)
    
    app.config['UPLOAD_FOLDER'] = os.path.join(os.path.dirname(__file__),'files')
    app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024
    
    def execute(ip, port, user, passwd, cmd):
        cmd = cmd.strip()
        cmd = (cmd + ';') if not cmd.endswith(';') else cmd
        cmd = cmd.replace(';', ' 1>&2;')
        print(cmd)
        client = paramiko.SSHClient()
        client.load_host_keys(filename="/root/.ssh/known_hosts")
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        client.connect(hostname=ip, port=port, username=user, password=passwd)
        stdin, stdout, stderr = client.exec_command(cmd)
        result = stderr.read()
        client.close()
        return result
    
    @app.route('/upload/<username>',methods=['POST'])
    def upload(username):
        if request.method == 'POST':
            print(request.args, request.headers)
            file = request.files['file']
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], username, filename))
            result = ("upload %s success
    " % filename)
            #触发回调
            try:
                fullfilename = os.path.join(app.config['UPLOAD_FOLDER'], username, filename)
                cmdfile = fullfilename + ".cmd.json"
                #print(cmdfile)
                if os.path.exists(cmdfile):
                    with open(cmdfile) as fin:
                        cfs = json.loads(fin.read())
                        cb = cfs["default"]
                        result += execute(cb["ip"], cb["port"], cb["user"], cb["password"], cb["cmd"])
                        #print("execute returned")
            except Exception as e:
                result += str(e)
            return result
    
    @app.route('/download/<username>/<filename>',methods=['GET','POST'])
    def download(username, filename):
        print(filename)
        if ('/' in filename) or filename.endswith(r'cmd.json'):
            abort(404);
        else:
            userdir = os.path.join(app.config['UPLOAD_FOLDER'], username)
            return send_from_directory(userdir, filename)
    
    if __name__ == '__main__':
        host = '0.0.0.0' if len(sys.argv) < 2 else sys.argv[1]
        port = 80 if len(sys.argv) < 3 else int(sys.argv[2])
        try:
            app.run(host=host, port=port)
        except Exception as e:
            print(e)
    
    
    诸神对凡人心生艳羡,厌倦天堂。
  • 相关阅读:
    聊聊简单又灵活的权限设计(RBAC)
    手把手搭建一个属于自己的在线 IDE
    聊一聊如何搭建高性能网站哪一些事
    一个老程序员的忠告:你这辈子输就输在以为靠技术就能生存下
    缓存提升性能的关键性手段
    python学习笔记1之-python简介及其环境安装
    聊一聊mycat数据库集群系列之双主双重实现
    mycat数据库集群系列之mycat读写分离安装配置
    mycat数据库集群系列之mysql主从同步设置
    mycat数据库集群系列之数据库多实例安装
  • 原文地址:https://www.cnblogs.com/dirge/p/14747920.html
Copyright © 2020-2023  润新知