使用python将wave文件从客户端上载到服务器

使用python将wave文件从客户端上载到服务器,python,flask,Python,Flask,我需要从客户端向服务器发送一个wave文件, 在服务器端接收文件进行一些处理并将其转换为文本文件, 然后将文本文件发送回服务器。 我不应该用插座。 我是python新手,对如何完成任务知之甚少。 我在客户端使用了以下代码: import os # We'll render HTML templates and access data sent by POST # using the request object from flask. Redirect and url_for # will be

我需要从客户端向服务器发送一个wave文件, 在服务器端接收文件进行一些处理并将其转换为文本文件, 然后将文本文件发送回服务器。 我不应该用插座。 我是python新手,对如何完成任务知之甚少。 我在客户端使用了以下代码:

import os
# We'll render HTML templates and access data sent by POST
# using the request object from flask. Redirect and url_for
# will be used to redirect the user once the upload is done
# and send_from_directory will help us to send/show on the
# browser the file that the user just uploaded
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename

# Initialize the Flask application
app = Flask(__name__)

# This is the path to the upload directory
app.config['UPLOAD_FOLDER'] = 'uploads/'
# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'wav'])

# For a given file, return whether it's an allowed type or not
def allowed_file(test):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']

# This route will show a form to perform an AJAX request
# jQuery is loaded to execute the request and update the
# value of the operation
@app.route('/')
def index():
    return render_template('index.html')


# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
    # Get the name of the uploaded file
    file = request.files['file']
    # Check if the file is one of the allowed types/extensions
    if file and allowed_file(file.filename):
        # Make the filename safe, remove unsupported chars
        filename = secure_filename(file.filename)
        # Move the file form the temporal folder to
        # the upload folder we setup
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # Redirect the user to the uploaded_file route, which
        # will basicaly show on the browser the uploaded file
        return redirect(url_for('uploaded_file',
                                filename=filename))

# This route is expecting a parameter containing the name
# of a file. Then it will locate that file on the upload
# directory and show it on the browser, so if the user uploads
# an image, that image is going to be show after the upload
@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename)

if __name__ == '__main__':
    app.run(
        host="0.0.0.0",
        port=int("80"),
        debug=True
    )
导入操作系统
#我们将呈现HTML模板并访问通过POST发送的数据
#使用flask中的请求对象。重定向和url_以用于
#将用于在上载完成后重定向用户
#和从_目录发送_将帮助我们在
#浏览用户刚刚上载的文件
从flask导入flask,呈现模板,请求,重定向,url,从目录发送
从werkzeug导入安全文件名
#初始化烧瓶应用程序
app=烧瓶(名称)
#这是上传目录的路径
app.config['UPLOAD_FOLDER']='uploads/'
#这些是我们正在接受上传的扩展
app.config['ALLOWED_EXTENSIONS']=set(['txt',pdf',png',jpg',jpeg',gif',wav'])
#对于给定的文件,返回是否为允许的类型
允许的def_文件(测试):
在文件名和\
app.config['ALLOWED_EXTENSIONS']中的filename.rsplit('.',1)[1]
#此路由将显示执行AJAX请求的表单
#jQuery被加载以执行请求并更新
#操作的价值
@应用程序路径(“/”)
def index():
返回渲染模板('index.html')
#将处理文件上载的路由
@app.route('/upload',methods=['POST'])
def upload():
#获取上载文件的名称
file=request.files['file']
#检查文件是否为允许的类型/扩展名之一
如果文件和允许的文件(file.filename):
#确保文件名安全,删除不支持的字符
filename=secure\u文件名(file.filename)
#将文件从临时文件夹移动到
#我们设置的上载文件夹
保存(os.path.join(app.config['UPLOAD\u FOLDER'],文件名))
#将用户重定向到上载的_文件路由,该路由
#基本上我会在浏览器上显示上传的文件吗
返回重定向(url_用于('上载的_文件'),
文件名=文件名)
#此路由需要包含名称的参数
#一个文件的名称。然后它会在上传中找到该文件
#目录并在浏览器上显示,因此如果用户上载
#一张图片,上传后将显示该图片
@app.route(“/uploads/”)
def上传文件(文件名):
从_目录返回发送_(app.config['UPLOAD_FOLDER'],
文件名)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(
host=“0.0.0.0”,
端口=int(“80”),
调试=真
)
我不确定是否将主机从“0.0.0.0”更改为服务器的Ip地址? 该代码是否足以从客户端发送wave文件? 如何在服务器端接收文件?
有人能帮忙吗?

在服务器端,您可以使用关联数组
$\u FILES
接收文件。然后,您必须移动上载的文件,然后才能将其处理为文本文件

我需要从客户端向服务器发送一个wave文件,在服务器端接收该文件进行一些处理并将其转换为文本文件,然后将文本文件发送回服务器

我想你是说

[…]然后将文本文件发送回客户端

要做到这一点,只需在Python中打开url作为发送文件的标题,它将被处理为文本,然后在网页上打印文本。使用Python,您可以将整个页面读入字符串并完成

伪代码:

wav_to_text = openurl("http://www.example.com/wav_to_text.php", file="files/example.wav")

text = wav_to_text.readpage()

print(text)

# ... Do what you want with the text