跳转到主要内容

适用于HTTP应用的简单路由系统

项目描述

http-router – 适用于HTTP应用的简单路由器

该库不是一个HTTP框架。它是一个用于构建框架的工具。该库的主要目标是绑定目标到HTTP路由并将它们匹配。

Tests Status PYPI Version Python Versions

要求

  • python 3.8, 3.9, 3.10, 3.11, 3.12, pypy3

安装

http-router 应使用pip进行安装

pip install http-router

使用

创建路由器

from http_router import Router


# Initialize the router
router = Router(trim_last_slash=True)

定义路由

@router.route('/simple')
def simple():
    return 'result from the fn'

使用HTTP路径和可选方法调用路由器以获取匹配结果。

match = router('/simple', method='GET')
assert match, 'HTTP path is ok'
assert match.target is simple

路由器还支持正则表达式对象

import re

@router.route(re.compile(r'/regexp/\w{3}-\d{2}/?'))
def regex():
    return 'result from the fn'

但是该库为动态路由提供了一个更简单的接口

@router.route('/users/{username}')
def users():
    return 'result from the fn'

默认情况下,这将捕获路径末尾或下一个/之前的字符。

可选地,您可以使用转换器来指定参数的类型,例如 {variable_name:converter}

转换器类型

str

(默认) 接受不带斜杠的任何文本

int

接受正整数

float

接受正浮点数

path

像字符串一样,但也可以接受斜杠

uuid

接受UUID字符串

使用冒号前缀来使用转换器,例如这样

@router.route('/orders/{order_id:int}')
def orders():
    return 'result from the fn'

任何未知的转换器都将被解析为正则表达式

@router.route('/orders/{order_id:\d{3}}')
def orders():
    return 'result from the fn'

也支持多个路径

@router.route('/', '/home')
def index():
    return 'index'

处理HTTP方法

@router.route('/only-post', methods=['POST'])
def only_post():
    return 'only-post'

子路由挂载

subrouter = Router()

@subrouter.route('/items/{item}')
def items():
     pass

 router = Router()
 router.route('/api')(subrouter)


match = router('/api/items/12', method='GET')
assert match, 'HTTP path is ok'
assert match.target is items
 assert match.params == {"item": "12"}

错误追踪

如果您有任何建议、错误报告或不满,请向https://github.com/klen/http-router/issues的问题跟踪器报告

贡献

项目开发发生在:https://github.com/klen/http-router

许可证

许可协议为MIT许可

项目详情


下载文件

下载适合您平台的文件。如果您不确定该选择哪个,请了解有关安装包的更多信息。

源分发

http-router-4.1.2.tar.gz (9.7 kB 查看散列值)

上传时间:

构建分发

http_router-4.1.2-cp312-cp312-win_amd64.whl (299.5 kB 查看散列值)

上传时间: CPython 3.12 Windows x86-64

http_router-4.1.2-cp312-cp312-musllinux_1_1_x86_64.whl (913.4 kB 查看散列值)

上传时间: CPython 3.12 musllinux: musl 1.1+ x86-64

http_router-4.1.2-cp312-cp312-musllinux_1_1_aarch64.whl (899.6 kB 查看散列值)

上传时间: CPython 3.12 musllinux: musl 1.1+ ARM64

http_router-4.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (897.2 kB 查看散列值)

上传时间: CPython 3.12 manylinux: glibc 2.17+ ARM64

http_router-4.1.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (912.1 kB 查看散列值)

上传于 CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

http_router-4.1.2-cp312-cp312-macosx_10_9_x86_64.whl (310.6 kB 查看哈希)

上传于 CPython 3.12 macOS 10.9+ x86-64

http_router-4.1.2-cp312-cp312-macosx_10_9_universal2.whl (416.6 kB 查看哈希)

上传于 CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

http_router-4.1.2-cp311-cp311-win_amd64.whl (300.0 kB 查看哈希)

上传于 CPython 3.11 Windows x86-64

http_router-4.1.2-cp311-cp311-musllinux_1_1_x86_64.whl (910.6 kB 查看哈希)

上传于 CPython 3.11 musllinux: musl 1.1+ x86-64

http_router-4.1.2-cp311-cp311-musllinux_1_1_aarch64.whl (901.1 kB 查看哈希)

上传于 CPython 3.11 musllinux: musl 1.1+ ARM64

http_router-4.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (905.3 kB 查看哈希)

上传于 CPython 3.11 manylinux: glibc 2.17+ x86-64

http_router-4.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (898.9 kB 查看哈希)

上传于 CPython 3.11 manylinux: glibc 2.17+ ARM64

http_router-4.1.2-cp311-cp311-macosx_10_9_x86_64.whl (310.7 kB 查看哈希)

上传于 CPython 3.11 macOS 10.9+ x86-64

http_router-4.1.2-cp311-cp311-macosx_10_9_universal2.whl (416.4 kB 查看哈希)

上传于 CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

http_router-4.1.2-cp310-cp310-win_amd64.whl (299.9 kB 查看哈希)

上传于 CPython 3.10 Windows x86-64

http_router-4.1.2-cp310-cp310-musllinux_1_1_x86_64.whl (853.2 kB 查看哈希值)

上传时间: CPython 3.10 musllinux: musl 1.1+ x86-64

http_router-4.1.2-cp310-cp310-musllinux_1_1_aarch64.whl (841.6 kB 查看哈希值)

上传时间: CPython 3.10 musllinux: musl 1.1+ ARM64

http_router-4.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (843.8 kB 查看哈希值)

上传时间: CPython 3.10 manylinux: glibc 2.17+ x86-64

http_router-4.1.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (835.6 kB 查看哈希值)

上传时间: CPython 3.10 manylinux: glibc 2.17+ ARM64

http_router-4.1.2-cp310-cp310-macosx_10_9_x86_64.whl (310.4 kB 查看哈希值)

上传时间: CPython 3.10 macOS 10.9+ x86-64

http_router-4.1.2-cp310-cp310-macosx_10_9_universal2.whl (415.5 kB 查看哈希值)

上传时间: CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

http_router-4.1.2-cp39-cp39-win_amd64.whl (300.4 kB 查看哈希值)

上传时间: CPython 3.9 Windows x86-64

http_router-4.1.2-cp39-cp39-musllinux_1_1_x86_64.whl (850.7 kB 查看哈希值)

上传时间: CPython 3.9 musllinux: musl 1.1+ x86-64

http_router-4.1.2-cp39-cp39-musllinux_1_1_aarch64.whl (842.1 kB 查看哈希值)

上传时间: CPython 3.9 musllinux: musl 1.1+ ARM64

http_router-4.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (843.5 kB 查看哈希值)

上传时间: CPython 3.9 manylinux: glibc 2.17+ x86-64

http_router-4.1.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (835.6 kB 查看哈希值)

上传于 CPython 3.9 manylinux: glibc 2.17+ ARM64

http_router-4.1.2-cp39-cp39-macosx_10_9_x86_64.whl (310.7 kB 查看哈希值)

上传于 CPython 3.9 macOS 10.9+ x86-64

http_router-4.1.2-cp39-cp39-macosx_10_9_universal2.whl (416.2 kB 查看哈希值)

上传于 CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

http_router-4.1.2-cp38-cp38-win_amd64.whl (300.7 kB 查看哈希值)

上传于 CPython 3.8 Windows x86-64

http_router-4.1.2-cp38-cp38-musllinux_1_1_x86_64.whl (884.1 kB 查看哈希值)

上传于 CPython 3.8 musllinux: musl 1.1+ x86-64

http_router-4.1.2-cp38-cp38-musllinux_1_1_aarch64.whl (875.1 kB 查看哈希值)

上传于 CPython 3.8 musllinux: musl 1.1+ ARM64

http_router-4.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (854.7 kB 查看哈希值)

上传于 CPython 3.8 manylinux: glibc 2.17+ x86-64

http_router-4.1.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (846.9 kB 查看哈希值)

上传于 CPython 3.8 manylinux: glibc 2.17+ ARM64

http_router-4.1.2-cp38-cp38-macosx_10_9_x86_64.whl (310.7 kB 查看哈希值)

上传于 CPython 3.8 macOS 10.9+ x86-64

http_router-4.1.2-cp38-cp38-macosx_10_9_universal2.whl (416.0 kB 查看哈希值)

上传于 CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面