from string import Template import pprint redirect_url = "http://tfkyle.dyndns.org/~kyle/test2/untest.py" realm = "test1" unauthed_tmpl = Template(""" Unauthorized

If you're seeing this make sure your client supports Basic authentication

""") def unauthorized(env, start): status = "401 Unauthorized" response_headers = [ ("WWW-Authenticate", 'Basic realm="%s"' % realm), ("Content-Type", "text/html"), ] start(status, response_headers) return [unauthed_tmpl.substitute(pretty_env=pprint.pformat(env))] def redirect(env, start): status = "302 Found" response_headers = [ ("Location", redirect_url), ("Content-Type", "text/plain"), ] start(status, response_headers) return ["redirecting to %s ..." % redirect_url] def application(env, start): if "HTTP_AUTHORIZATION" not in env: return unauthorized(env, start) auth = env["HTTP_AUTHORIZATION"] scheme, auth = auth.split(None, 1) if scheme.lower() != "basic": return unauthorized(env, start) return redirect(env, start)