我正在尝试在 Flask 应用程序中从服务器设置客户端内容。这是我的代码
# Importing flask module in the project is mandatory
# An object of Flask class is our WSGI application.
from flask import Flask, send_file, Response, render_template, redirect, url_for, request
import random
import time
import wave
import io
import os
# Flask constructor takes the name of
# current module (__name__) as argument.
app = Flask(__name__)
# The route() function of the Flask class is a decorator,
# which tells the application which URL should call
# the associated function.
# ‘/’ URL is bound with index() function.
@app.route('/')
def paadas():
#Approach 2
def generate(files):
with wave.open(files[0], 'rb') as f:
params = f.getparams()
frames = f.readframes(f.getnframes())
for file in files[1:]:
with wave.open(file, 'rb') as f:
frames += f.readframes(f.getnframes())
buffer = io.BytesIO()
with wave.open(buffer, 'wb') as f:
f.setparams(params)
f.writeframes(frames)
buffer.seek(0)
return buffer.read()
files = []
number = random.randint(1,10)
files.append("./numbers/" + str(number) + ".wav")
times = random.randint(1,10)
files.append("./times/" + str(times) + ".wav")
data = dict(
file=(generate(files), "padaa.wav"),
)
app.post(url_for('static', filename='padaa.wav'), content_type='multipart/form-data', data=data)
print ('posted data on client\n')
return render_template("index.html")
@app.route("/recording", methods=['POST', 'GET'])
def index():
if request.method == "POST":
f = open('./file.wav', 'wb')
print(request)
f.write(request.data)
f.close()
if os.path.isfile('./file.wav'):
print("./file.wav exists")
return render_template('index.html', request="POST")
else:
return render_template("index.html")
# main driver function
if __name__ == '__main__':
# run() method of Flask class runs the application
# on the local development server.
app.run()
但是在执行 app.post 时失败,错误为
AssertionError: The setup method 'post' can no longer be called on the application. It has already handled its first request, any changes will not be applied consistently.
Make sure all imports, decorators, functions, etc. needed to set up the application are done before running it.
这是完整的堆栈:
Traceback (most recent call last):
File "C:\ML\Tables\app\webapp\venv\lib\site-packages\flask\app.py", line 2213, in __call__
return self.wsgi_app(environ, start_response)
File "C:\ML\Tables\app\webapp\venv\lib\site-packages\flask\app.py", line 2193, in wsgi_app
response = self.handle_exception(e)
File "C:\ML\Tables\app\webapp\venv\lib\site-packages\flask\app.py", line 2190, in wsgi_app
response = self.full_dispatch_request()
File "C:\ML\Tables\app\webapp\venv\lib\site-packages\flask\app.py", line 1486, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\ML\Tables\app\webapp\venv\lib\site-packages\flask\app.py", line 1484, in full_dispatch_request
rv = self.dispatch_request()
File "C:\ML\Tables\app\webapp\venv\lib\site-packages\flask\app.py", line 1469, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "C:\ML\Tables\app\PaadasMLFlaskApp\PaadasML.py", line 47, in paadas
app.post(url_for('static', filename='padaa.wav'), content_type='multipart/form-data', data=data)
File "C:\ML\Tables\app\webapp\venv\lib\site-packages\flask\scaffold.py", line 50, in wrapper_func
self._check_setup_finished(f_name)
File "C:\ML\Tables\app\webapp\venv\lib\site-packages\flask\app.py", line 511, in _check_setup_finished
raise AssertionError(
我错过了什么?我怎样才能做到这一点?如果您想试用,可以从 https://github.com/sameermahajan/PaadasMLFlaskApp 获得我完整的工作应用程序。只需取消注释 python 文件中的 app.post... 语句即可重现错误。
基本上,当我从 paadas() 返回时,我想播放音频,就像
所做的那样return Response(generate(files), mimetype='audio/wav')
但是想要关注 index.html 以便我可以继续我的交互。