关于这个特定的TypeError有很多帖子,但对我来说似乎没有任何帮助。我有一个函数,它从服务器返回一个文件,但每次我试图调用它时,都会发生这个错误:
文件";services.py";,75号线,装饰返回f(用户,*args,**kwargs(
TypeError:downloadanddelete((为参数"file"获取了多个值
我在网上能找到的只是关于类函数和重写位置参数的问题。
测试建议或指向有关该问题的在线资源将不胜感激。
services.py(主(
from downlaod import downlaoad
from functools import wraps
from flask import *
from flask_sqlalchemy import SQLAlchemy
from apscheduler.schedulers.background import BackgroundScheduler
# create app
app = Flask(__name__)
# config for app --> link database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://////opt/services/databases/users.db'
db = SQLAlchemy(app)
User = database(db=db)
def auth(f):
@wraps(f)
def decorated(*args, **kwargs):
#request authorization and save data in auth
auth = request.authorization
# if auth or username or password are empty
if not auth or not auth.username or not auth.password:
return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})
# find the user
user = User.query.filter_by(username=auth.username).first()
# if user is empty
if not user:
return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})
# if password is not correct
if user.password != auth.password:
return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})
# if password is correct
if user.password == auth.password:
# gib user zurück
return f(user, *args, **kwargs)
return decorated
@app.route('/download/<file>', methods=['GET'])
@auth
def downloadanddelete(file,current_app):
r = downlaoad(file=file,current_app=current_app)
return r
if __name__ == '__main__':
app.run(port=8081, host="127.0.0.1")
下载并删除.py
# import Lib
from flask import make_response, Flask, send_file
import os
def downlaod(file,current_app):
# path of temp
path = '/opt/services/temp/' + file
# generate a file in the ram than delete the file from SSD or HDD
def generate():
with open(path, "rb") as f:
yield from f
os.remove(path)
r = current_app.response_class(generate())
r.headers.set('Content-Disposition', 'attachment', file)
#send file
return r
更新#1:尝试在本地复制错误,并使用了与上述完全相同的代码。我得到了一个不同的错误:
类型错误:downloadanddelete((缺少1个必需的位置参数:"current_app">
我没有将current_app
传递给downloadanddelete()
,而是从烧瓶中导入:,从而使它在Windows机器上本地工作
服务.py更改
@app.route('/download/<file>', methods=['GET'])
def downloadanddelete(file):
return downlaod(file=file)
下载并删除.py
from flask import make_response, Flask, current_app
import os
def downlaod(file):
path = 'C:\Users\my_username\temp'
def generate():
with open(path, "rb") as f:
yield from f
os.remove(path)
r = current_app.response_class(generate())
r.headers.set('Content-Disposition', 'attachment', filename=file)
return r
我对服务器进行了同样的更改,得到了与以前完全相同的错误:
文件";services.py";,第75行,修饰返回f(user,*args,**kwargs(
TypeError:downloadanddelete((为参数"file"获取了多个值
我目前正在尝试在本地复制错误,有什么想法吗?
因此,事后看来,解决方案非常简单,但代码的编写方式可能会混淆。auth()
函数返回一个用户对象。因此,当您使用@app.route
和@auth
时,函数期望当前用户。如果被调用的函数(downloadanddelete(中没有用户参数,它将在第一个位置参数中传递,这就是出现错误的原因。
解决方案
@app.route('/download/<file>', methods=['GET'])
@auth
def downloadanddelete(user,file,current_app):
r = downlaoad(user=user,file=file,current_app=current_app)
return r