summaryrefslogtreecommitdiffhomepage
path: root/test/python/delayed/wsgi.py
blob: 3eb5a6f80e1faea76921cd6695bd581218302e79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import time


def application(environ, start_response):
    parts = int(environ.get('HTTP_X_PARTS', 1))
    delay = int(environ.get('HTTP_X_DELAY', 0))

    content_length = int(environ.get('CONTENT_LENGTH', 0))
    body = bytes(environ['wsgi.input'].read(content_length))

    write = start_response('200', [('Content-Length', str(len(body)))])

    if not body:
        time.sleep(delay)
        return []

    step = int(len(body) / parts)
    for i in range(0, len(body), step):
        try:
            write(body[i : i + step])
        except:
            break

        time.sleep(delay)

    return []