我正在尝试创建一个临时Excel文件,可通过运行在PythonAnywhere上的Flask(v0.12)Web应用程序下载该文件,如下所示:
from io import BytesIO
from openpyxl import Workbook
from flask import send_file
from datetime import date
# ... Flask app here
# inside function/route serving Excel file
buffer = BytesIO()
wb = Workbook()
ws = wb.active
ws.title = "Sample"
# populate sample data
ws["A1"] = "some data"
ws["A2"] = str(date.today())
# send file to requestor
wb.save(buffer)
buffer.seek(0)
return send_file(
buffer,
as_attachment=True,
attachment_filename="{}_workbook.xlsx".format(str(date.today())),
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
我遇到此异常:
SystemError: <built-in function uwsgi_sendfile> returned a result with an error set
我不知道该如何解决。
完整追溯:
2018-09-10 18:00:43,816: Error running WSGI application
2018-09-10 18:00:43,845: SystemError: <built-in function uwsgi_sendfile> returned a result with an error set
2018-09-10 18:00:43,846: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1994, in __call__
2018-09-10 18:00:43,846: return self.wsgi_app(environ, start_response)
2018-09-10 18:00:43,846:
2018-09-10 18:00:43,846: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
2018-09-10 18:00:43,846: response = self.handle_exception(e)
2018-09-10 18:00:43,847:
2018-09-10 18:00:43,847: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
2018-09-10 18:00:43,847: reraise(exc_type, exc_value, tb)
2018-09-10 18:00:43,847:
2018-09-10 18:00:43,847: File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-09-10 18:00:43,847: raise value
2018-09-10 18:00:43,847:
2018-09-10 18:00:43,848: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
2018-09-10 18:00:43,848: response = self.full_dispatch_request()
2018-09-10 18:00:43,848:
2018-09-10 18:00:43,848: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
2018-09-10 18:00:43,848: rv = self.handle_user_exception(e)
2018-09-10 18:00:43,848:
2018-09-10 18:00:43,848: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
2018-09-10 18:00:43,849: reraise(exc_type, exc_value, tb)
2018-09-10 18:00:43,849:
2018-09-10 18:00:43,849: File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-09-10 18:00:43,849: raise value
2018-09-10 18:00:43,849:
2018-09-10 18:00:43,849: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
2018-09-10 18:00:43,849: rv = self.dispatch_request()
2018-09-10 18:00:43,850:
2018-09-10 18:00:43,850: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
2018-09-10 18:00:43,850: return self.view_functions[rule.endpoint](**req.view_args)
2018-09-10 18:00:43,850:
2018-09-10 18:00:43,850: File "/usr/local/lib/python3.6/dist-packages/flask_login/utils.py", line 228, in decorated_view
2018-09-10 18:00:43,850: return func(*args, **kwargs)
2018-09-10 18:00:43,851:
2018-09-10 18:00:43,851: File "/home/ecwhitetailranch/public_html/app.py", line 1560, in inventory_report
2018-09-10 18:00:43,851: return send_file(
2018-09-10 18:00:43,851:
2018-09-10 18:00:43,851: File "/usr/local/lib/python3.6/dist-packages/flask/helpers.py", line 553, in send_file
2018-09-10 18:00:43,851: data = wrap_file(request.environ, file)
2018-09-10 18:00:43,851:
2018-09-10 18:00:43,852: File "/usr/local/lib/python3.6/dist-packages/werkzeug/wsgi.py", line 726, in wrap_file
2018-09-10 18:00:43,852: return environ.get('wsgi.file_wrapper', FileWrapper)(file, buffer_size)
答案 0 :(得分:3)
这里的问题是PythonAnywhere的uwsgi配置与Python 3.5+中的send_file
文件包装有关。 This is a known issue. This issue就是您尝试显示的样子。
要解决此问题,您需要在wsgi-disable-file-wrapper=true
文件中添加uwsgi.ini
标志(但我不认为您有权在PythonAnywhere上执行此操作)。