Python 如何处理烧瓶中的流式post请求?
我想通过post请求将csv文件从node.js服务器传输到Flask应用程序。以下是生成请求的node.js代码:Python 如何处理烧瓶中的流式post请求?,python,node.js,flask,stream,wsgi,Python,Node.js,Flask,Stream,Wsgi,我想通过post请求将csv文件从node.js服务器传输到Flask应用程序。以下是生成请求的node.js代码: // node.js route that handles form-based csv file upload app.post('/route', upload.single('file'), function(req,res) { var file = req.file; // Streams file to Flask app API
// node.js route that handles form-based csv file upload
app.post('/route', upload.single('file'), function(req,res) {
var file = req.file;
// Streams file to Flask app API
fs.createReadStream(file).pipe(
request.post('http://localhost:5000/upload_dataset',
function(error, response, body){
console.log(body);
})
);
});
这是Flask请求处理程序:
@app.route('/upload_dataset', methods=['POST'])
def upload_dataset():
# Read from the request buffer and write it to a new file
with open(app.root_path + "/tmp/current_dataset", "bw") as f:
chunk_size = 4096
while True:
chunk = request.stream.read(chunk_size)
if len(chunk) == 0:
return
f.write(chunk)
return 'Some Response'
问题是,当路由被命中时,len(chunk)为0,因此不会写入任何内容
Q1)请求的传输编码头类型为“chunked”。我听说wsgi不处理“分块”请求。这就是导致我的问题的原因吗?如果是的话,除了目前的实施,什么是好的替代方案
Q2)request.stream似乎为空。请求文件是否可能以请求对象的不同参数结尾?如果是,哪一个
否则,如果您发现代码有任何其他问题,请告诉我
谢谢。Flask直接支持文件上传,只需使用它,而不是重新实现它。
upload.single('file')
做什么?request.post中的主体在哪里?
?pvg:Flask对上传的支持似乎仅限于表单介导的上传,这就是request.file为空的原因。我需要能够将文件从node.js应用程序流式传输到flask,而不是从html表单。wolfsgang:upload.single.('file')只是一些处理文件上传到node的中间件。它是multer的一部分,一个用于解析多部分数据的node.js包。我对node不太熟悉,但似乎有很多问题和文档指针涉及到了这一点。