Skip to content

Instantly share code, notes, and snippets.

@nickstenning
Created October 2, 2011 13:24
Show Gist options
  • Save nickstenning/1257451 to your computer and use it in GitHub Desktop.
Save nickstenning/1257451 to your computer and use it in GitHub Desktop.
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