xref: /unit/test/python/ctx_iter_atexit/wsgi.py (revision 602:3b36a1d5b47b)
1import atexit
2
3class application:
4    def __init__(self, environ, start_response):
5        self.environ = environ
6        self.start = start_response
7
8    def __iter__(self):
9        atexit.register(self._atexit)
10
11        content_length = int(self.environ.get('CONTENT_LENGTH', 0))
12        body = bytes(self.environ['wsgi.input'].read(content_length))
13
14        self.start('200', [
15            ('Content-Type', self.environ.get('CONTENT_TYPE')),
16            ('Content-Length', str(len(body)))
17        ])
18        yield body
19
20    def _atexit(self):
21        self.start('200', [
22            ('Content-Length', '0')
23        ])
24