【问题标题】:Error occurred in mobile when send video using send_file in Python Flask使用 Python Flask 中的 send_file 发送视频时,移动设备发生错误
【发布时间】:2013-10-25 14:32:59
【问题描述】:

我正在使用 Python Flask。我想通过权限检查向用户发送媒体文件(mp4、mp3、mov 等)。我将使用数据库检查用户权限,并且我只想为经过身份验证的用户发送文件。

所以我开始开发那个系统。我使用登录检查权限,我使用 Send_file 向用户发送文件。在桌面上,它运行良好。但在我的 iPad、iPhone、Android 手机上,它不起作用。他们的播放器警报“无法播放此视频”。在 iPhone 中,播放器的播放按钮不可用。

iPhone 错误截图here http://webhost.swisscom.co.kr/temp/stackoverflow_iphone1.jpg

Flask 服务器(Gunicorn)返回

“错误:[Errno 107] 传输端点未连接”。 python flask调试服务器时-》报错:[Error 32] Broken pipe.

我在超过 5 个不同的服务器上进行了测试,但仍然无法正常工作。

我还使用了 x-sendfile 或 send_from_directory,手动构建响应标头。

@app.route('/download/<path:filename>')
def download(filename):
    return send_file('data/file/' + filename)

在 iOS7、iOS6、android 中请求视频时出现 Gunicorn Flask Server 错误

2013-10-17 16:38:46 [14344] [ERROR] Error processing request.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 88, in handle
    self.handle_request(listener, req, client, addr)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 145, in handle_request
    client.shutdown(socket.SHUT_RDWR)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 107] Transport endpoint is not connected
2013-10-17 16:38:47 [14331] [ERROR] Error processing request.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 88, in handle
    self.handle_request(listener, req, client, addr)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 145, in handle_request
    client.shutdown(socket.SHUT_RDWR)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 107] Transport endpoint is not connected

在 iOS7 或 iOS6 或 android 中请求视频时 Python 调试器服务器错误

123.123.123.123 - - [17/Oct/2013 16:34:36] "GET /download/welcome.mp4 HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('110.70.52.201', 38723)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 704, in finish
    self.wfile.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

【问题讨论】:

    标签: python flask gunicorn


    【解决方案1】:

    我终于解决了这个问题。

    首先,我将 Nginx 与 Gunicorn 一起使用。

    在使用 Nginx 和 Gunicorn 时,您可以在下载文件之前使用以下步骤检查 auth 信息。

    它仅使用 Nginx 内部重定向功能。它将阻止未经身份验证的访问您的文件。

    用户 -> Nginx -> Gunicorn -> 检查 Python 代码中的 Auth 信息 -> 响应数据

    Nginx 配置

    server {
        listen 80;
        server_name yourserver.com;
    
        location /download_file/ {
            internal; # Receive just internal redirect
            alias    /file/path/in/your/server/;
        }
    
        # Just for Gunicorn Serving (option)
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header Host $http_host;
             proxy_redirect off;
    
             if (!-f $request_filename) {
                 proxy_pass http://service;
                 break;
             }
        }
    }
    

    Python 代码

    @app.route('/download/<path:filename>')
    def download(filename):
        if not authentication():
            abort(404)
    
        redirect_path = '/download_file/' + filename
    
        response = make_response("")
        response.headers["X-Accel-Redirect"] = redirect_path
        response.headers["Content-Type"] = mimetypes.guess_type(filename)
        return response
    

    然后你的浏览器会收到来自 Nginx 的文件

    还在 iOS 中工作喜欢从通用 Web 服务器提供服务。

    来自

    的信息

    http://mattspitz.me/2013/11/20/serving-authenticated-media-with-nginx.html

    【讨论】:

    • 达兹是对的。 Gunicorn 不处理文件中的搜索。然而,Nginx 确实如此,并且您通过让 nginx 处理自己提供的文件来做正确的事情。感谢您的链接!
    【解决方案2】:

    为了在 iOS 设备上支持流媒体,Web 服务器需要支持基于传入范围标头发送 206 响应(部分内容)。有一些 send_file 替换示例可以为您处理此问题,例如邮件列表中的 this onethis one(注意:我自己没有尝试过这些确切的示例,但已经为此实现了类似的东西 - 所以 iOS 会正确播放视频)

    【讨论】:

    • 我已经适应了我的代码这两个源,但它仍然显示错误。在 python 调试器中仍然显示“Broken Pipe”或无法使用 Gunicorn 在 iOS 中播放
    猜你喜欢
    • 2017-09-24
    • 2020-11-11
    • 2016-03-15
    • 1970-01-01
    • 2013-09-07
    • 2022-01-21
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    相关资源
    最近更新 更多