• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
2# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
3# (c) 2005 Clark C. Evans
4# This module is part of the Python Paste Project and is released under
5# the MIT License: http://www.opensource.org/licenses/mit-license.php
6# This code was written with funding by http://prometheusresearch.com
7"""
8Various Applications for Debugging/Testing Purposes
9"""
10
11import time
12__all__ = ['SimpleApplication', 'SlowConsumer']
13
14
15class SimpleApplication(object):
16    """
17    Produces a simple web page
18    """
19    def __call__(self, environ, start_response):
20        body = b"<html><body>simple</body></html>"
21        start_response("200 OK", [('Content-Type', 'text/html'),
22                                  ('Content-Length', str(len(body)))])
23        return [body]
24
25class SlowConsumer(object):
26    """
27    Consumes an upload slowly...
28
29    NOTE: This should use the iterator form of ``wsgi.input``,
30          but it isn't implemented in paste.httpserver.
31    """
32    def __init__(self, chunk_size = 4096, delay = 1, progress = True):
33        self.chunk_size = chunk_size
34        self.delay = delay
35        self.progress = True
36
37    def __call__(self, environ, start_response):
38        size = 0
39        total  = environ.get('CONTENT_LENGTH')
40        if total:
41            remaining = int(total)
42            while remaining > 0:
43                if self.progress:
44                    print("%s of %s remaining" % (remaining, total))
45                if remaining > 4096:
46                    chunk = environ['wsgi.input'].read(4096)
47                else:
48                    chunk = environ['wsgi.input'].read(remaining)
49                if not chunk:
50                    break
51                size += len(chunk)
52                remaining -= len(chunk)
53                if self.delay:
54                    time.sleep(self.delay)
55            body = "<html><body>%d bytes</body></html>" % size
56        else:
57            body = ('<html><body>\n'
58                '<form method="post" enctype="multipart/form-data">\n'
59                '<input type="file" name="file">\n'
60                '<input type="submit" >\n'
61                '</form></body></html>\n')
62        print("bingles")
63        start_response("200 OK", [('Content-Type', 'text/html'),
64                                  ('Content-Length', len(body))])
65        return [body]
66
67def make_test_app(global_conf):
68    return SimpleApplication()
69
70make_test_app.__doc__ = SimpleApplication.__doc__
71
72def make_slow_app(global_conf, chunk_size=4096, delay=1, progress=True):
73    from paste.deploy.converters import asbool
74    return SlowConsumer(
75        chunk_size=int(chunk_size),
76        delay=int(delay),
77        progress=asbool(progress))
78
79make_slow_app.__doc__ = SlowConsumer.__doc__
80