Python Falcon路由与简单函数?

2024-06-01 10:47:06 发布

您现在位置:Python中文网/ 问答频道 /正文

以入门页中的以下示例为例

我可以将路由处理程序编写为简单函数吗

没有类、类实例、成员函数、自实例引用

# things.py

# Let's get this party started!
import falcon


# Falcon follows the REST architectural style, meaning (among
# other things) that you think in terms of resources and state
# transitions, which map to HTTP verbs.
class ThingsResource(object):
    def on_get(self, req, resp):
        """Handles GET requests"""
        resp.status = falcon.HTTP_200  # This is the default status
        resp.body = ('\nTwo things awe me most, the starry sky '
                     'above me and the moral law within me.\n'
                     '\n'
                     '    ~ Immanuel Kant\n\n')

# falcon.API instances are callable WSGI apps
app = falcon.API()

# Resources are represented by long-lived class instances
things = ThingsResource()

# things will handle all requests to the '/things' URL path
app.add_route('/things', things)

Tags: andtheto实例函数httpgetstatus