I have a simple Python server that uses http.server . The goal is not to display the video on an html page, or to upload a video file, but directly display the video in a browser. This is what I have so far:
import http.server class SuperHandler(http.server.SimpleHTTPRequestHandler): def do_GET(self): path = self.path encodedFilePath = 'file.mp4' with open(encodedFilePath, 'rb') as videoFile: self.send_response(200) self.send_header('Content-type', 'video/mp4') self.end_headers() self.wfile.write(videoFile.read()) print('File sent: ' + videoFile.name) server_address = ('', 8000) handler_class = SuperHandler httpd = http.server.HTTPServer(server_address, handler_class) httpd.serve_forever()
The problem is that the answer does not contain the full video. file.mp4 is 50 MB, but when I look at the Chrome or Firefox network tab, it says that the response is only 1 MB. Is there a reason the full file is not migrated? Do I need to add some kind of HTTP header to do this?
EDIT:
Now this is my code:
server_address = ('', 8000) handler_class = http.server.SimpleHTTPRequestHandler httpd = http.server.HTTPServer(server_address, handler_class) httpd.serve_forever()
Now I use the default SimpleHTTPRequestHandler do_GET , but it still does not work (although the answer now is 40 MB / 30 MB, not 1 MB).
When I request file.mp4 in Chrome, the socket connection closes after ~ 7 seconds (~ 5 seconds in Firefox), which makes the script throw a BrokenPipeError: [Errno 32] Broken pipe , because the server is still trying to write the rest of the video file in a private socket .
So my question is: how can I get the browser to load the full answer before it closes the socket?
Additional Information
HTTP response headers sent to the client:
HTTP/1.0 200 OK Server: SimpleHTTP/0.6 Python/3.5.0 Date: Mon, 28 Dec 2015 02:36:39 GMT Content-type: video/mp4 Content-Length: 53038876 Last-Modified: Fri, 25 Dec 2015 02:09:52 GMT