Python Falcon 简明教程

Python Falcon - Inspect Module

inspect 模块是一个方便的工具,它提供有关注册路由和其他 Falcon 应用程序组件(例如中间件、吸收器等)的信息。

The inspect module is a handy tool that provides information about registered routes and other components of a Falcon application such as middleware, sinks etc.

可以通过两种方式检查应用程序 - CLI 工具和以编程方式。从命令行执行 falcon-inspect -tool CLI 脚本,其中给出了申报 Falcon 应用程序对象的 Python 脚本的名称。

The inspection of an application can be done by two ways – CLI tool and programmatically. The falcon-inspect-tool CLI script is executed from the command line giving the name of Python script in which Falcon application object is declared.

例如,如要检查 studentapi.py 中的应用程序对象 -

For example, to inspect application object in studentapi.py

falcon-inspect-app studentapi:app
Falcon App (WSGI)
Routes:
   ⇒ /students - StudentResource:
   ├── GET - on_get
   └── POST - on_post
   ⇒ /students/{id:int} - StudentResource:
   ├── DELETE - on_delete_student
   ├── GET - on_get_student
   └── PUT - on_put_student

输出显示注册的路由和资源类中的响应程序方法。要以编程方式执行检查,请使用 inspect 模块中的 inspect_app() 函数将应用程序对象作为参数。

The output shows registered routes and the responder methods in the resource class. To perform the inspection programmatically, use the application object as argument to inspect_app() function in the inspect module.

from falcon import inspect
from studentapi import app
app_info = inspect.inspect_app(app)
print(app_info)

将上述脚本另存为 inspectapi.py,然后从命令行运行它。

Save the above script as inspectapi.py and run it from the command line.

python inspectapi.py
Falcon App (WSGI)
Routes:
   ⇒ /students - StudentResource:
   ├── GET - on_get
   └── POST - on_post
   ⇒ /students/{id:int} - StudentResource:
   ├── DELETE - on_delete_student
   ├── GET - on_get_student
   └── PUT - on_put_student