我尝试将图像上传到python Flask API。
Flask代码基于以下代码片段和教程:
关于这个主题,我已经尝试过stackoverflow的不同解决方案,但是它们都不能解决问题。在
我的烧瓶代码@app.route("/rupload/", methods=['POST', 'PUT'])
def rupload(filename):
# Sanity checks and setup skipped.
filename = secure_filename(filename)
fileFullPath = os.path.join(UPLOAD_FOLDER, filename)
with open(fileFullPath, 'wb') as f:
f.write(request.get_data())
return jsonify({
'filename': filename,
'size': os.path.getsize(fileFullPath)
})
脚本
文件测试.jpg与此脚本位于同一文件夹中。我试图使用python代码复制curl -v -H 'Content-Type: application/octet-stream' -X POST --data @test.jpg localhost:5000/rupload/test.jpg的效果,因为我不想使用curl。在
^{pr2}$
运行此脚本时(Flask应用程序也在运行),它会引发以下错误:Traceback (most recent call last):
File "", line 10, in
requests.post(url,headers=headers,data='file.jpg')
File "C:\Program Files\Python35\lib\site-packages\requests\api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "C:\Program Files\Python35\lib\site-packages\requests\sessions.py", line 612, in send
adapter = self.get_adapter(url=request.url)
File "C:\Program Files\Python35\lib\site-packages\requests\sessions.py", line 703, in get_adapter
raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for 'localhost:5000/rupload/test'
我不太确定如何解决那个问题。到目前为止,我尝试的所有方法都没有奏效。
我猜API工作正常(它没有识别请求,也没有抛出错误),错误完全是由脚本引起的。API的所有其他部分(此处未显示)都能正常工作。如果有人能为我指出解决这个问题的方向,那就太好了。在
谢谢