如何在pythonweb请求中获取auth_用户
我是python新手。我想在一个web项目中创建一个基本的身份验证,有没有任何方法可以获取HTTP身份验证变量,比如php中的“$”服务器['php\u auth\u user']”?如何在pythonweb请求中获取auth_用户,python,Python,我是python新手。我想在一个web项目中创建一个基本的身份验证,有没有任何方法可以获取HTTP身份验证变量,比如php中的“$”服务器['php\u auth\u user']”? 我使用的是FF的torando服务器。Tornado中似乎没有任何对基本身份验证的特殊支持,因此您必须自己通过base64解码授权头来完成 可能是这样的: import base64 class BasicAuthHandler(tornado.web.RequestHandler): def get_
我使用的是FF的torando服务器。Tornado中似乎没有任何对基本身份验证的特殊支持,因此您必须自己通过base64解码
授权
头来完成
可能是这样的:
import base64
class BasicAuthHandler(tornado.web.RequestHandler):
def get_current_user(self):
scheme, _, token= self.request.headers.get('Authorization', '').partition(' ')
if scheme.lower()=='basic':
user, _, pwd= base64.decodestring(token).partition(':')
# if pwd matches user:
return user
return None
def get(self):
if not self.get_current_user():
self.set_status(401)
self.set_header('WWW-Authenticate', 'basic realm="Example site"')
# produce error/login page for user to see if they press escape to
# cancel authorisation
return
(由于我没有运行Tornado,所以未进行测试。)该模块为Tornado提供基本和摘要身份验证。一个简单的用法示例是:
import tornado.ioloop
from tornado_http_auth import BasicAuthMixin
from tornado.web import RequestHandler, Application
class MainHandler(BasicAuthMixin, RequestHandler):
def prepare(self):
self.get_authenticated_user(realm='Protected', auth_func=credentials.get)
def get(self):
self.write('Hello %s' % self._current_user)
app = Application([
(r'/', MainHandler),
])
app.listen(8080)
tornado.ioloop.IOLoop.current().start()