Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@validator
def app(environ, start_response):
"""Simplest possible application object"""
status = '200 OK'
response_headers = [
('Content-type', 'text/plain'),
('Content-Length', length),
]
start_response(status, response_headers)
return iter([message])
def test_wsgi_input(self):
def bad_app(e,s):
e["wsgi.input"].read()
s("200 OK", [("Content-Type", "text/plain; charset=utf-8")])
return [b"data"]
out, err = run_amock(validator(bad_app))
self.assertTrue(out.endswith(
b"A server error occurred. Please contact the administrator."
))
self.assertEqual(
err.splitlines()[-2], "AssertionError"
)
def StartWebServer(self, port):
"""Start web server."""
application = self.CreateWSGIApplication()
validated_application = validate.validator(application)
server = simple_server.make_server('localhost', port, validated_application)
server = ServerThread(server)
server.start()
server.wait_until_running()
return server, application
def test():
from wsgiref.validate import validator
magics = StringMagic(title="String Test"), KidMagic(title="Kid Test")
app = Shock('testdata/pub', magics=magics)
try:
make_server('localhost', 9999, validator(app)).serve_forever()
except KeyboardInterrupt, ki:
print "Ciao, baby!"
name, cgi.escape(`environ[name]`))
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ,
keep_blank_values=1)
if form.list:
yield 'Form data'
for field in form.list:
yield '%s%s\n' % (
field.name, field.value)
yield '\n' \
'\n'
from wsgiref import validate
test_app = validate.validator(test_app)
WSGIServer(test_app).run()
name, cgi.escape(`environ[name]`))
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ,
keep_blank_values=1)
if form.list:
yield 'Form data'
for field in form.list:
yield '%s%s\n' % (
field.name, field.value)
yield '\n' \
'\n'
from wsgiref import validate
test_app = validate.validator(test_app)
# Explicitly set bindAddress to *:8009 for testing.
WSGIServer(test_app,
bindAddress=('', 8009), allowedServers=None,
loggingLevel=logging.DEBUG).run()
name, cgi.escape(`environ[name]`))
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ,
keep_blank_values=1)
if form.list:
yield 'Form data'
for field in form.list:
yield '%s%s\n' % (
field.name, field.value)
yield '\n' \
'\n'
from wsgiref import validate
test_app = validate.validator(test_app)
WSGIServer(test_app,
loggingLevel=logging.DEBUG).run()
name, cgi.escape(`environ[name]`))
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ,
keep_blank_values=1)
if form.list:
yield 'Form data'
for field in form.list:
yield '%s%s\n' % (
field.name, field.value)
yield '\n' \
'\n'
from wsgiref import validate
test_app = validate.validator(test_app)
# Explicitly set bindAddress to *:4001 for testing.
WSGIServer(test_app,
bindAddress=('', 4001), allowedServers=None,
loggingLevel=logging.DEBUG).run()
$ wsgicli run hello.py app -h 0.0.0.0 -p 5000 --reload
$ wsgicli run hello.py app --static --static-root /static/ --static-dirs ./static/
"""
insert_import_path_to_sys_modules(filepath)
module = SourceFileLoader('module', filepath).load_module()
app = getattr(module, wsgiapp)
if static:
from wsgi_static_middleware import StaticMiddleware
app = StaticMiddleware(app, static_root=static_root, static_dirs=static_dirs)
if validate:
from wsgiref.validate import validator
app = validator(app)
if lineprof:
# Caution: wsgi-lineprof is still pre-alpha. Except breaking API Changes.
from wsgi_lineprof.middleware import LineProfilerMiddleware
from wsgi_lineprof.filters import FilenameFilter, TotalTimeSorter
if lineprof_file:
# Now wsgi-lineprof is now supported only 1 file checking.
lineprof_file = lineprof_file[0]
else:
lineprof_file = os.path.basename(filepath)
filters = [FilenameFilter(lineprof_file), TotalTimeSorter()]
app = LineProfilerMiddleware(app, filters=filters)
if reload:
run_live_reloading_server(interval, app=app, host=host, port=port)