使用Flask返回服务器上的server.log文件

时间:2018-11-23 06:06:13

标签: python-3.x flask

我正在使用python 3和flask服务器。该服务器可以浏览文件并将其上传到名为“ uploads”的文件夹

所有处理后,结果保存到'server.log'

我想向Android应用显示结果。为此,我想将server.log返回到服务器,以便可以由Android应用程序访问。

任何帮助都将受到赞赏。预先感谢。

代码如下:

import os
from flask import Flask, request, redirect, url_for, send_from_directory, jsonify
from werkzeug import secure_filename

UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'mp4'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
  # this has changed from the original example because the original did not work for me
    return filename[-3:].lower() in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    print("Printing the request {}".format(request.headers))
    print("Printing the request {}".format(request))
    print("Printing the request {}".format(request.data))
    print("Printing the request {}".format(request.form))
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            print('**found file', file.filename)
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            # for browser, add 'redirect' function on top of 'url_for'
            return jsonify({"success" : url_for('uploaded_file',
                                    filename=filename)})
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''

@app.route('/uploads/<filename>')   #this part of server.py is missing from video attendance output.py 
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

if __name__ == '__main__':
    app.run(host= '192.168.0.102', debug=True)

0 个答案:

没有答案