龙卷风的文摘与基本认证
tornado-http-auth的Python项目详细描述
Tornadoweb框架的摘要和基本身份验证。基于代码 还有来自Twisted的想法。
安装
最新稳定版本的tornado ansi标记可以从pypi安装:
$ pip install tornado-http-auth
用法
importtornado.ioloopfromtornado.webimportRequestHandler,Applicationfromtornado_http_authimportDigestAuthMixin,BasicAuthMixin,auth_requiredcredentials={'user1':'pass1'}# Example 1 (using decorator).classMainHandler(DigestAuthMixin,RequestHandler):@auth_required(realm='Protected',auth_func=credentials.get)defget(self):self.write('Hello %s'%self._current_user)# Example 2 (using prepare and get_authentciated_user).classMainHandler(BasicAuthMixin,RequestHandler):defprepare(self):self.get_authenticated_user(check_credentials_func=credentials.get,realm='Protected')defget(self):self.write('Hello %s'%self._current_user)app=Application([(r'/',MainHandler),])app.listen(8888)tornado.ioloop.IOLoop.current().start()# curl --user user1:pass1 -v http://localhost:8888 -> 200 OK# curl --user user2:pass2 -v http://localhost:8888 -> 401 Unauthorized# Remove or comment second class# curl --digest --user user1:pass1 -v http://localhost:8888 -> 200 OK# curl --digest --user user2:pass2 -v http://localhost:8888 -> 401 Unauthorized
许可证
这个项目是根据Apache License, Version 2.0的条款发布的。