summaryrefslogtreecommitdiffhomepage
path: root/test/python/upload/wsgi.py
blob: 953c5ecc869d319ca6cd6ea5444717442df4a8cd (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
27
28
29
from tempfile import TemporaryFile
import os, cgi


def read(environ):
    length = int(environ.get('CONTENT_LENGTH', 0))

    body = TemporaryFile(mode='w+b')
    body.write(bytes(environ['wsgi.input'].read(length)))
    body.seek(0)

    environ['wsgi.input'] = body
    return body


def application(environ, start_response):
    file = read(environ)

    form = cgi.FieldStorage(fp=file, environ=environ, keep_blank_values=True)

    filename = form['file'].filename
    data = filename.encode() + form['file'].file.read()

    start_response(
        '200 OK',
        [('Content-Type', 'text/plain'), ('Content-Length', str(len(data)))],
    )

    return data