English 中文(简体)
Python Falcon - Inspect Module
  • 时间:2024-11-03

Python Falcon - Inspect Module


Previous Page Next Page  

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

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

For example, to inspect apppcation 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

The output shows registered routes and the responder methods in the resource class. To perform the inspection programmatically, use the apppcation 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)

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


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
Advertisements