Skip to content

Instantly share code, notes, and snippets.

@nickstenning
Created October 2, 2011 13:24

Revisions

  1. nickstenning revised this gist Oct 2, 2011. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions stream.py
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,10 @@

    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)
    @@ -20,6 +24,7 @@ def _counter(n):
    yield '%d\n' % i
    time.sleep(1)

    response.headers['Content-Type'] = 'text/plain'
    response.headers['Transfer-Encoding'] = 'chunked'

    return chunky(_counter(int(num)))
  2. nickstenning created this gist Oct 2, 2011.
    25 changes: 25 additions & 0 deletions stream.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    from streamapp.lib.base import BaseController

    from pylons import response

    import time

    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['Transfer-Encoding'] = 'chunked'

    return chunky(_counter(int(num)))