使用表单/cookie控制访问的最简单的wsgi中间件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用表单/cookie控制访问的最简单的wsgi中间件相关的知识,希望对你有一定的参考价值。
Not very secure, but very handy ...Use like that :
app = your_wsgi_app()
app = ControlAccess( app, "your_password")
httpserve( app ) # wsgi server
import Cookie,hashlib md5 = lambda x : hashlib.md5(x).hexdigest() class ControlAccess: def __init__(self, appReal,password): self.appReal = appReal self.password=password def __call__(self, environ, start_response): try: password = Cookie.SimpleCookie(environ.get("HTTP_COOKIE",""))["pass"].value except: password = "" if password==md5(self.password): for i in self.appReal(environ, start_response): yield i else: try: passw=environ['wsgi.input'].read(int(environ['CONTENT_LENGTH'])).split("=")[-1] except: passw="" if passw == self.password: cookie = Cookie.SimpleCookie() cookie["pass"] = md5(self.password) start_response('200 OK', [('Content-type','text/html'),('Set-Cookie',cookie["pass"].OutputString())]) yield """<html><head><meta http-equiv="refresh" content="0; url=/" /></head><body>welcome</body></html>""" else: start_response('200 OK', [('Content-type','text/html')]) yield """<form method="post"> Password <input type='password' name="password"> <input type='submit' value="ok"> </form>"""
以上是关于使用表单/cookie控制访问的最简单的wsgi中间件的主要内容,如果未能解决你的问题,请参考以下文章
将 AngularJS 范围变量从指令传递到控制器的最简单方法?