Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added simple yield server demonstrating streaming content to multiple…
… clients.
- Loading branch information
1 parent
3ba2a96
commit 8e21f69
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
layout: default | ||
title: How to Stream Large Files | ||
--- | ||
|
||
# How to Stream Large Files | ||
|
||
This is an example of how you can use web.py to stream large files. You'll find it DOES work as advertised, but there are some protocol changes you may have to make to stream files of certain types to certain clients. | ||
|
||
You need the content type and chunked header to make things show up correctly on a typical web browser. | ||
|
||
Simple Example | ||
import web | ||
import time | ||
|
||
urls = ( | ||
"/", "count_holder", | ||
"/(.*)", "count_down", | ||
) | ||
app = web.application(urls, globals()) | ||
|
||
|
||
class count_down: | ||
def GET(self,count): | ||
web.header('Content-type','text/html') | ||
web.header('Transfer-Encoding','chunked') | ||
yield '<h2>Prepare for Launch!</h2>' | ||
j = '<li>Liftoff in %s...</li>' | ||
yield '<ul>' | ||
count = int(count) | ||
for i in range(count,0,-1): | ||
out = j % i | ||
time.sleep(1) | ||
yield out | ||
yield '</ul>' | ||
time.sleep(1) | ||
yield '<h1>Lift off</h1>' | ||
|
||
class count_holder: | ||
def GET(self): | ||
web.header('Content-type','text/html') | ||
web.header('Transfer-Encoding','chunked') | ||
boxes = 4 | ||
delay = 3 | ||
countdown = 10 | ||
for i in range(boxes): | ||
output = '<iframe src="/%d" width="200" height="500"></iframe>' % (countdown - i) | ||
yield output | ||
time.sleep(delay) | ||
|
||
if __name__ == "__main__": | ||
app.run() |