Created
October 2, 2011 13:24
-
-
Save nickstenning/1257451 to your computer and use it in GitHub Desktop.
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
from streamapp.lib.base import BaseController | |
from pylons import response | |
import time | |
# This just works, but if we're being really good we should probably set | |
# protocol_version = HTTP/1.1 | |
# in the server:main section of the config file. | |
def chunky(iterable): | |
def _chunkify(chunk): | |
return '%s\r\n%s\r\n' % (hex(len(chunk))[2:], chunk) | |
for chunk in iterable: | |
yield _chunkify(chunk) | |
yield '0\r\n\r\n' | |
class StreamController(BaseController): | |
def count(self, num): | |
def _counter(n): | |
for i in xrange(n): | |
yield '%d\n' % i | |
time.sleep(1) | |
response.headers['Content-Type'] = 'text/plain' | |
response.headers['Transfer-Encoding'] = 'chunked' | |
return chunky(_counter(int(num))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment