Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 65 lines (50 sloc) 2.055 kB
8e21f69 @dublo7 Added simple yield server demonstrating streaming content to multiple…
dublo7 authored
1 ---
2 layout: default
3 title: How to Stream Large Files
4 ---
5
6 # How to Stream Large Files
7
9c46f58 Add link to french translation, add "problem" and "solution" for coo…
nux authored
8 Other languages: [français](/../cookbook/streaming_large_files/fr) | ...
9
10 ## Problem:
11
12 You want to use web.py to stream large files.
13
14 ##Solution:
15
ef78ac6 @dublo7 trying to explain yield better... too tired right now. hope this mak…
dublo7 authored
16 This is an example of how you can use web.py to stream large files. You'll find it DOES work as advertised, but you need to make sure you add the Transfer-Encoding chunked header for it to display properly. Otherwise the browser will buffer all data before displaying it to you.
17
18 You can't mix basic string and Yield returns in the same method. If you use Yield, you'll have to use yield for everything because your function becomes a generator.
8e21f69 @dublo7 Added simple yield server demonstrating streaming content to multiple…
dublo7 authored
19
20
9c46f58 Add link to french translation, add "problem" and "solution" for coo…
nux authored
21 ##Simple Example:
8e21f69 @dublo7 Added simple yield server demonstrating streaming content to multiple…
dublo7 authored
22
7e527a4 @dublo7 fixed the code formatting I think
dublo7 authored
23 # Simple streaming server demonstration
ef78ac6 @dublo7 trying to explain yield better... too tired right now. hope this mak…
dublo7 authored
24 # Uses time.sleep to emulate a large file read
7e527a4 @dublo7 fixed the code formatting I think
dublo7 authored
25 import web
26 import time
27
28 urls = (
29 "/", "count_holder",
30 "/(.*)", "count_down",
31 )
32 app = web.application(urls, globals())
33
34
35 class count_down:
36 def GET(self,count):
ef78ac6 @dublo7 trying to explain yield better... too tired right now. hope this mak…
dublo7 authored
37 # These headers make it work in browsers
7e527a4 @dublo7 fixed the code formatting I think
dublo7 authored
38 web.header('Content-type','text/html')
39 web.header('Transfer-Encoding','chunked')
40 yield '<h2>Prepare for Launch!</h2>'
41 j = '<li>Liftoff in %s...</li>'
42 yield '<ul>'
43 count = int(count)
44 for i in range(count,0,-1):
45 out = j % i
46 time.sleep(1)
47 yield out
48 yield '</ul>'
8e21f69 @dublo7 Added simple yield server demonstrating streaming content to multiple…
dublo7 authored
49 time.sleep(1)
7e527a4 @dublo7 fixed the code formatting I think
dublo7 authored
50 yield '<h1>Lift off</h1>'
51
52 class count_holder:
53 def GET(self):
54 web.header('Content-type','text/html')
55 web.header('Transfer-Encoding','chunked')
56 boxes = 4
57 delay = 3
58 countdown = 10
59 for i in range(boxes):
60 output = '<iframe src="/%d" width="200" height="500"></iframe>' % (countdown - i)
61 yield output
62 time.sleep(delay)
63
64 if __name__ == "__main__":
65 app.run()
Something went wrong with that request. Please try again.