summaryrefslogtreecommitdiff
path: root/paste/debug/debugapp.py
blob: c33e1f4f31ce7e3a7bd433a94c4ac2791f2cc96d (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# (c) 2005 Clark C. Evans
# This module is part of the Python Paste Project and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
# This code was written with funding by http://prometheusresearch.com
"""
Various Applications for Debugging/Testing Purposes
"""

import time
__all__ = ['SimpleApplication', 'SlowConsumer']


class SimpleApplication:
    """
    Produces a simple web page
    """
    def __call__(self, environ, start_response):
        body = "<html><body>simple</body></html>"
        start_response("200 OK",[('Content-Type','text/html'),
                                 ('Content-Length',len(body))])
        return [body]

class SlowConsumer:
    """
    Consumes an upload slowly...

    NOTE: This should use the iterator form of ``wsgi.input``,
          but it isn't implemented in paste.httpserver.
    """
    def __init__(self, chunk_size = 4096, delay = 1, progress = True):
        self.chunk_size = chunk_size
        self.delay = delay
        self.progress = True

    def __call__(self, environ, start_response):
        size = 0
        total  = environ.get('CONTENT_LENGTH')
        if total:
            remaining = int(total)
            while remaining > 0:
                if self.progress:
                    print "%s of %s remaining" % (remaining, total)
                if remaining > 4096:
                    chunk = environ['wsgi.input'].read(4096)
                else:
                    chunk = environ['wsgi.input'].read(remaining)
                if not chunk:
                    break
                size += len(chunk)
                remaining -= len(chunk)
                if self.delay:
                    time.sleep(self.delay)
            body = "<html><body>%d bytes</body></html>" % size
        else:
            body = ('<html><body>\n'
                '<form method="post" enctype="multipart/form-data">\n'
                '<input type="file" name="file">\n'
                '<input type="submit" >\n'
                '</form></body></html>\n')
        print "bingles"
        start_response("200 OK",[('Content-Type', 'text/html'),
                                 ('Content-Length', len(body))])
        return [body]