使用 python Falcon 进行路由
Routing with python Falcon
我是 python 的 Falcon 框架的新手。我对 Falcon 的中间件 class 的使用有疑问。在中间件中使用自定义路由器和请求身份验证是否明智,还是应该仅在路由上处理
**main.py**
import falcon
import falcon_jsonify
import root
from waitress import serve
if __name__ == "__main__":
app = falcon.API(
middleware=[falcon_jsonify.Middleware(help_messages=True),
root.customRequestParser()]
)
serve(app, host="0.0.0.0", port=5555)
root.py
我计划在其中编写自定义路由
import json
import falcon
class Home(object):
@classmethod
def getResponse(self):
return {"someValue": "someOtherValue"}
def process_request_path(path):
path = path.lstrip("/").split("/")
return path
class customRequestParser(object):
def process_request(self, req, resp):
print process_request_path(req.path)
我还看到了使用 app = falcon.API(router=CustomRouter())
的示例。我在 falcon 官方文档页面上看到了一个文档 - http://falcon.readthedocs.io/en/stable/api/routing.html
如果有任何参考文献可以让我知道,请告诉我。
How do I authenticate requests?
Hooks and middleware components can be used together to authenticate and authorize requests. For example, a middleware component could be used to parse incoming credentials and place the results in req.context
. Downstream components or hooks could then use this information to authorize the request, taking into account the user’s role and the requested resource.
Falcon 的 Hooks 是用于特定请求函数(即 on_get
)或整个 class 的装饰器。它们非常适合验证传入的请求,因此如常见问题解答所述,此时可以完成身份验证。
这是我编写的一个(未经测试的)示例:
def AuthParsingMiddleware(object):
def process_request(self, req, resp):
req.context['GodMode'] = req.headers.get('Auth-Token') == 'GodToken':
# Might need process_resource & process_response
def validate_god_mode(req, resp, resource, params):
if not req.context['GodMode']:
raise falcon.HTTPBadRequest('Not authorized', 'You are not god')
def GodLikeResource(object):
@falcon.before(validate_god_mode):
def on_get(self, req, resp):
resp.body = 'You have god mode; I prostrate myself'
app = falcon.API(
middleware=[falcon_jsonify.Middleware(help_messages=True),
AuthParsingMiddleware()]
)
app.add_route('/godlikeresource', GodLikeResource())
或者更好...
我是 python 的 Falcon 框架的新手。我对 Falcon 的中间件 class 的使用有疑问。在中间件中使用自定义路由器和请求身份验证是否明智,还是应该仅在路由上处理
**main.py**
import falcon
import falcon_jsonify
import root
from waitress import serve
if __name__ == "__main__":
app = falcon.API(
middleware=[falcon_jsonify.Middleware(help_messages=True),
root.customRequestParser()]
)
serve(app, host="0.0.0.0", port=5555)
root.py
我计划在其中编写自定义路由
import json
import falcon
class Home(object):
@classmethod
def getResponse(self):
return {"someValue": "someOtherValue"}
def process_request_path(path):
path = path.lstrip("/").split("/")
return path
class customRequestParser(object):
def process_request(self, req, resp):
print process_request_path(req.path)
我还看到了使用 app = falcon.API(router=CustomRouter())
的示例。我在 falcon 官方文档页面上看到了一个文档 - http://falcon.readthedocs.io/en/stable/api/routing.html
如果有任何参考文献可以让我知道,请告诉我。
How do I authenticate requests?
Hooks and middleware components can be used together to authenticate and authorize requests. For example, a middleware component could be used to parse incoming credentials and place the results in
req.context
. Downstream components or hooks could then use this information to authorize the request, taking into account the user’s role and the requested resource.
Falcon 的 Hooks 是用于特定请求函数(即 on_get
)或整个 class 的装饰器。它们非常适合验证传入的请求,因此如常见问题解答所述,此时可以完成身份验证。
这是我编写的一个(未经测试的)示例:
def AuthParsingMiddleware(object):
def process_request(self, req, resp):
req.context['GodMode'] = req.headers.get('Auth-Token') == 'GodToken':
# Might need process_resource & process_response
def validate_god_mode(req, resp, resource, params):
if not req.context['GodMode']:
raise falcon.HTTPBadRequest('Not authorized', 'You are not god')
def GodLikeResource(object):
@falcon.before(validate_god_mode):
def on_get(self, req, resp):
resp.body = 'You have god mode; I prostrate myself'
app = falcon.API(
middleware=[falcon_jsonify.Middleware(help_messages=True),
AuthParsingMiddleware()]
)
app.add_route('/godlikeresource', GodLikeResource())