Skip to main content
 首页 » 编程设计

python之在 Python Flask 中使用 send_file 发送视频时手机出现错误

2024年11月01日8Terrylee

我正在使用 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) 返回

"error: [Errno 107] Transport endpoint is not connected". When python flask debugging server - "error: [Error 32] Broken pipe.

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

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

@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 

请您参考如下方法:

我终于解决了这个问题。

首先,我将 Nginx 与 Gunicorn 结合使用。

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

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

用户 -> Nginx -> Gunicorn -> 在 python 代码中检查授权信息 -> 响应数据

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 中工作也喜欢从通用网络服务器提供服务。

信息来自

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