txwsgi / doc / examples / demo_write.py

#!/usr/bin/python
# Copyright (c) 2010 Manlio Perillo (manlio.perillo@gmail.com).
# See LICENSE for details.

from cgi import parse_qs

from twisted.internet import reactor
from twisted.application import service, strports
from twisted.web import server

from txwsgi import WSGIResource, write_adapter


BUFSIZE = 1024
N = 50

def wsgi_application(environ, start_response):
    """A simple WSGI application that use the write callable and
    return a generator.

    This example show usage of the C{txwsgi.greenlet.write_adapter}
    adapter.
    """

    def app_iter():
        # write('foo')

        for i in range(ord('a'), ord('z') + 1):
            yield chr(i) * bufsize + '\n'

    headers = [('Content-Type', 'text/plain')]

    qs = environ.get('QUERY_STRING', '')
    args = parse_qs(qs)
    bufsize = int(args.get('bufsize', [BUFSIZE])[0])
    n = int(args.get('n', [N])[0])

    write = start_response('200 OK', headers)
    for i in range(0, n + 1):
        write('%02d' % i * bufsize + '\n')

    return app_iter()


res = WSGIResource(reactor, wsgi_application, [write_adapter])
site = server.Site(res)
application = service.Application('demo write')

s = strports.service('tcp:8080', site)
s.setServiceParent(application)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.