首页 > 解决方案 > 如何访问客户端存在的数据文件并进行处理?

问题描述

我想要达到的目标如下:

如何访问文件数据?- 我要求客户向我发送文件的位置,但这不起作用。请提出合适的方法。

API 的代码如下:

import pandas as pd
from flask import Flask, request,render_template
import os

app = Flask(__name__)

@app.route('/<path:fileN>', methods=['GET'])
def my_form_post(fileN):
    f=open(fileN,"r")
    #f=os.read(fileN,os.O_RDONLY)
    lines=f.readlines()
    ff=[]
    for line in lines:
        parts=line.split("\t")
        parts=parts[:7]
        ff.append(parts)
    pd.set_option("display.colheader_justify","center")
    df=pd.DataFrame(ff,columns=['Date', 'Start Time', 'Time', '# of Req', 'HTTP Method', 'Link','HTTP Response'])
    df=df[df['Link'].str.startswith('/ic/')==True]
    df.sort_values(by="Time", axis=0, ascending=False, inplace=True)
    df.reset_index(inplace=True)
    df.drop(['index'], axis=1, inplace=True)
    df.index+=1
    df=df.head(5)
    html=df.to_html()
    return html

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, threaded=True, debug=True)

标签: pythonapiflask

解决方案


要接收文件,您需要使用 POST 方法并从请求中检索文件,如下所示:

file = request.files['file']

请注意,仅当请求方法为 POST、PUT 或 PATCH 并且发布到请求的文件具有 enctype="multipart/form-data" 时,文件才会包含数据。否则它将为空。您可以在此处查看更多详细信息。


推荐阅读