Gunicorn/Flask 基于二维码图片错误

Gunicorn/Flask based QRcode image error

我在 Flask 项目中使用 QRCode 生成密码。代码简单

# works for werkzerg, not for gunicorn    
@expose('/qrcode/<string:text>')
@has_access
def qrcode(self, text):
    qr = qrcode.QRCode(
        version=4,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=1)
    qr.add_data(text)
    img = qr.make_image()

    byte_io = BytesIO()
    img.save(byte_io, 'PNG')
    byte_io.seek(0)

    return send_file(byte_io, mimetype="image/png")

flask-appbuilder 项目基于 flask 框架,具有不同的装饰器,例如 expose,而不是 route。

该代码在 Wergzeug 上运行良好,但是当我 运行 在我的生产服务器上使用 Gunicorn 时。它抛出以下错误:

2018-02-22 16:27:52,377:ERROR:gunicorn.error:Error handling request
Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/gunicorn/workers/sync.py", line 102, in handle_request
    resp.write_file(respiter)
  File "/usr/lib/pymodules/python2.7/gunicorn/http/wsgi.py", line 285, in write_file
    fileno = respiter.filelike.fileno()
UnsupportedOperation: fileno

我想 send_file 和 gunicorn 之间存在一些问题。有没有人有类似的问题和解决方案?

这是 Gunicorn 中的一个错误,在使用 pip 升级后。

sudo pip install -U gunicorn

该代码现在适用于 gunicorn。