「译文」如何编写 Python Web 框架(二)

简介: 「译文」如何编写 Python Web 框架(二)

「译文」如何编写 Python Web 框架(一) ,我们开始编写自己的 Python 框架并实现以下功能:

  • WSGI 兼容
  • 请求处理程序
  • 路由:简单和参数化

请务必在此之前阅读系列的 「译文」如何编写 Python Web 框架(一)

这部分同样令人兴奋,我们将在其中添加以下功能:

  • 检查重复的路径
  • 基于类的处理程序
  • 单元测试

Ready? 让我们开始吧。

重复的路径

现在,我们的框架允许添加任意次数相同的路由。因此,以下内容将起作用:

@app.route("/home")
def home(request, response):
    response.text = "Hello from the HOME page"
@app.route("/home")
def home2(request, response):
    response.text = "Hello from the SECOND HOME page"
PYTHON

框架不会抱怨,因为我们使用 Python 字典来存储路由,只有最后一个才能使用 http://localhost:8000/home/。显然,这并不好。我们希望确保框架在用户尝试添加现有路由时会抛出信息。您可以想象,实施起来并不是很困难。因为我们使用 Python dict 来存储路由,所以我们可以简单地检查字典中是否已存在给定路径。如果是,我们抛出异常,如果不是,我们让它添加一个路由。在我们编写任何代码之前,让我们回忆下我们的主要API 类:

# api.py
class API:
    def __init__(self):
        self.routes = {}
    def route(self, path):
        def wrapper(handler):
            self.routes[path] = handler
            return handler
        return wrapper
    def __call__(self, environ, start_response):
        request = Request(environ)
        response = self.handle_request(request)
        return response(environ, start_response)
    def find_handler(self, request_path):
        for path, handler in self.routes.items():
            parse_result = parse(path, request_path)
            if parse_result is not None:
                return handler, parse_result.named
        return None, None
    def handle_request(self, request):
        response = Response()
        handler, kwargs = self.find_handler(request_path=request.path)
        if handler is not None:
            handler(request, response, **kwargs)
        else:
            self.default_response(response)
        return response
    def default_response(self, response):
        response.status_code = 404
        response.text = "Not found."
PYTHON

我们需要更改 route 函数,以便在再次添加现有路由时抛出异常:

# api.py
def route(self, path):
    if path in self.routes:
        raise AssertionError("Such route already exists.")
    def wrapper(handler):
        self.routes[path] = handler
        return handler
    return wrapper
PYTHON

现在,尝试添加相同的路径两次并重新启动你的 gunicorn。您应该看到抛出以下异常:

Traceback (most recent call last):
...
AssertionError: Such route already exists.
VIM

我们可以重构它以将其减少到一行:

# api.py
def route(self, path):
    assert path not in self.routes, "Such route already exists."
    ...
PYTHON

完工!进入下一个功能。

基于类的处理程序

如果你了解 Django,你知道它支持基于函数和基于类的视图(即我们的处理程序)。我们已经有了基于函数的处理程序。现在我们将添加基于类的,适用于更复杂, 更大的处理程序。我们基于类的处理程序将如下所示:

# app.py
@app.route("/book")
class BooksHandler:
    def get(self, req, resp):
        resp.text = "Books Page"
    def post(self, req, resp):
        resp.text = "Endpoint to create a book"
    ...
PYTHON

这意味着我们存储路径的 dict: self.routes可以包含类和函数作为值。因此,当我们在 handle_request() 方法中找到一个处理程序时,我们需要检查处理程序是一个函数还是一个类。如果它是一个函数,它应该像现在一样工作。如果它是一个类,根据请求方法,我们应该调用该类的对应方法。也就是说,如果请求方法是 GET,我们应该调用类的get() 方法,如果是 POST 我们应该调用 post 方法等。这是 handle_request() 方法现在的样子:

# api.py
def handle_request(self, request):
    response = Response()
    handler, kwargs = self.find_handler(request_path=request.path)
    if handler is not None:
        handler(request, response, **kwargs)
    else:
        self.default_response(response)
    return response
PYTHON

我们要做的第一件事是检查找到的处理程序是否是一个类。为此,我们使用 inspect 模块:

# api.py
import inspect
...
def handle_request(self, request):
    response = Response()
    handler, kwargs = self.find_handler(request_path=request.path)
    if handler is not None:
        if inspect.isclass(handler):
            pass   # class based handler is being used
        else:
            handler(request, response, **kwargs)
    else:
        self.default_response(response)
    return response
...
PYTHON

现在,如果正在使用基于类的处理程序,我们需要根据请求方法找到类的适当方法。为此,我们可以使用内置的 getattr 函数:

# api.py
def handle_request(self, request):
    response = Response()
    handler, kwargs = self.find_handler(request_path=request.path)
    if handler is not None:
        if inspect.isclass(handler):
            handler_function = getattr(handler(), request.method.lower(), None)
            pass
        else:
            handler(request, response, **kwargs)
    else:
        self.default_response(response)
    return response
PYTHON

getattr接受一个对象实例作为第一个参数,将属性名称作为第二个参数。第三个参数是如果没有找到则返回的值。因此,GET将返回 getPOST 返回 post, some_other_attribute 返回 None。如果handler_functionNone,则表示此类函数未在类中实现,并且不允许此请求方法:

if inspect.isclass(handler):
    handler_function = getattr(handler(), request.method.lower(), None)
    if handler_function is None:
        raise AttributeError("Method not allowed", request.method)
PYTHON

如果实际找到了 handler_function,那么我们只需调用它:

if inspect.isclass(handler):
    handler_function = getattr(handler(), request.method.lower(), None)
    if handler_function is None:
        raise AttributeError("Method now allowed", request.method)
    handler_function(request, response, **kwargs)
PYTHON

现在整个方法看起来像这样:

def handle_request(self, request):
    response = Response()
    handler, kwargs = self.find_handler(request_path=request.path)
    if handler is not None:
        if inspect.isclass(handler):
            handler_function = getattr(handler(), request.method.lower(), None)
            if handler_function is None:
                raise AttributeError("Method now allowed", request.method)
            handler_function(request, response, **kwargs)
        else:
            handler(request, response, **kwargs)
    else:
        self.default_response(response)
PYTHON

我不喜欢我们有两个 handler_functionhandler。我们可以重构它们以使它更优雅:

def handle_request(self, request):
    response = Response()
    handler, kwargs = self.find_handler(request_path=request.path)
    if handler is not None:
        if inspect.isclass(handler):
            handler = getattr(handler(), request.method.lower(), None)
            if handler is None:
                raise AttributeError("Method now allowed", request.method)
        handler(request, response, **kwargs)
    else:
        self.default_response(response)
    return response
PYTHON

就是这样。我们现在可以测试对基于类的处理程序的支持。首先,如果你还没有, 请将此处理程序添加到app.py

@app.route("/book")
class BooksHandler:
    def get(self, req, resp):
        resp.text = "Books Page"
PYTHON

现在,重新启动你的 gunicorn 并转到页面 http://localhost:8000/book,你应该看到消息Books Page。就这样, 我们增加了对基于类的处理程序的支持。可以试试实现其他方法(例如postdelete)。

进入下一个功能!

单元测试

如果没有单元测试,哪个项目是可靠的,对吧?所以让我们添加几个。我喜欢使用pytest,所以让我们安装它:

pip install pytest
SHELL

并创建一个文件,我们将编写测试:

touch test_bumbo.py
SHELL

提醒一下,bumbo是框架的名称。您可能以不同的方式命名。另外,如果您不知道 pytest 是什么,我强烈建议您查看它以了解如何编写单元测试。

首先,让我们为我们的 API 类创建一个我们可以在每个测试中使用的工具:

# test_bumbo.py
import pytest
from api import API
@pytest.fixture
def api():
    return API()
PYTHON

现在,对于我们的第一次单元测试,让我们从简单的开始。让我们测试一下我们是否可以添加路径。如果它没有抛出异常,则表示测试成功通过:

def test_basic_route(api):
    @api.route("/home")
    def home(req, resp):
        resp.text = "YOLO"
PYTHON

像这样运行测试:pytest test_bumbo.py你应该看到如下内容:

collected 1 item
test_bumbo.py .                                                                                                                                                            [100%]
====== 1 passed in 0.09 seconds ======
ABNF

现在,让我们测试它是否会在我们尝试添加现有路由时抛出异常:

# test_bumbo.py
def test_route_overlap_throws_exception(api):
    @api.route("/home")
    def home(req, resp):
        resp.text = "YOLO"
    with pytest.raises(AssertionError):
        @api.route("/home")
        def home2(req, resp):
            resp.text = "YOLO"
PYTHON

再次运行测试,您将看到它们都通过了。

我们可以添加更多测试,例如默认响应,参数化路由,状态代码等。但是,所有测试都要求我们向处理程序发送 HTTP 请求。为此,我们需要一个测试客户端。但是如果我们在这里做的话,我认为这篇文章会变得太大了。我们将在这些系列的下一篇文章中完成。我们还将添加对模板和其他一些有趣内容的支持。所以,请继续关注。

像往常一样,如果您想看一些功能实现,请在评论部分告诉我。

P.S. 这些博客文章基于我正在构建的 Python Web 框架。因此, 请在这儿 查看博客中的内容,一定要通过 star 该 repo 来表达你的喜爱。

Fight on!

相关文章
|
4天前
|
数据采集 XML 数据处理
使用Python实现简单的Web爬虫
本文将介绍如何使用Python编写一个简单的Web爬虫,用于抓取网页内容并进行简单的数据处理。通过学习本文,读者将了解Web爬虫的基本原理和Python爬虫库的使用方法。
|
1天前
|
机器学习/深度学习 前端开发 数据可视化
数据分析web可视化神器---streamlit框架,无需懂前端也能搭建出精美的web网站页面
数据分析web可视化神器---streamlit框架,无需懂前端也能搭建出精美的web网站页面
|
1天前
|
开发框架 前端开发 JavaScript
学会Web UI框架--Bootstrap,快速搭建出漂亮的前端界面
学会Web UI框架--Bootstrap,快速搭建出漂亮的前端界面
|
1天前
|
缓存 前端开发 安全
Python web框架fastapi中间件的使用,CORS跨域详解
Python web框架fastapi中间件的使用,CORS跨域详解
|
1天前
|
API 数据库 Python
Python web框架fastapi数据库操作ORM(二)增删改查逻辑实现方法
Python web框架fastapi数据库操作ORM(二)增删改查逻辑实现方法
|
1天前
|
关系型数据库 MySQL API
Python web框架fastapi数据库操作ORM(一)
Python web框架fastapi数据库操作ORM(一)
|
1天前
|
Python
python web框架fastapi模板渲染--Jinja2使用技巧总结
python web框架fastapi模板渲染--Jinja2使用技巧总结
|
1天前
|
开发框架 网络协议 前端开发
Python高性能web框架--Fastapi快速入门
Python高性能web框架--Fastapi快速入门
|
2天前
|
网络协议 数据库 开发者
构建高效Python Web应用:异步编程与Tornado框架
【4月更文挑战第29天】在Web开发领域,响应时间和并发处理能力是衡量应用性能的关键指标。Python作为一种广泛使用的编程语言,其异步编程特性为创建高性能Web服务提供了可能。本文将深入探讨Python中的异步编程概念,并介绍Tornado框架如何利用这一机制来提升Web应用的性能。通过实例分析,我们将了解如何在实际应用中实现高效的请求处理和I/O操作,以及如何优化数据库查询,以支持更高的并发用户数和更快的响应时间。
|
27天前
|
监控 JavaScript 前端开发
《理解 WebSocket:Java Web 开发的实时通信技术》
【4月更文挑战第4天】WebSocket是Java Web实时通信的关键技术,提供双向持久连接,实现低延迟、高效率的实时交互。适用于聊天应用、在线游戏、数据监控和即时通知。开发涉及服务器端实现、客户端连接及数据协议定义,注意安全、错误处理、性能和兼容性。随着实时应用需求增加,WebSocket在Java Web开发中的地位将更加重要。