使用Python Flask中的send_file发送视频时移动设备发生错误http://mattspitz.me/2013/11/20/serving-authenticated-media-with-nginx.html

问题描述 投票:2回答:2

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

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

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

Flask服务器(Gunicorn)回归

“错误:[Errno 107]传输端点未连接”。当python烧瓶调试服务器 - “错误:[错误32]管道损坏。

我在超过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
2个回答
1
投票

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


0
投票

我终于解决了这个问题。

首先,我正在使用Nginx和Gunicorn。

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

它使用的是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

© www.soinside.com 2019 - 2024. All rights reserved.