A simple router for HTTP applications
Project description
http-router – A simple router for HTTP applications
Requirements
python >= 3.7
Installation
http-router should be installed using pip:
pip install http-router
Usage
Create a router:
from http_router import Router
# Initialize the router
router = Router(trim_last_slash=True)
Define routes:
@router.route('/simple')
def simple():
return 'result from the fn'
Call the router with HTTP path and optionally method to get a match result.
match = router('/simple', method='GET')
assert match, 'HTTP path is ok'
assert match.target is simple
The router supports regex objects too:
import re
@router.route(re.compile(r'/regexp/\w{3}-\d{2}/?'))
def regex():
return 'result from the fn'
But the lib has a simplier interface for the dynamic routes:
@router.route('/users/{username}')
def users():
return 'result from the fn'
By default this will capture characters up to the end of the path or the next /.
Optionally, you can use a converter to specify the type of the argument like {variable_name:converter}.
Converter types:
str |
(default) accepts any text without a slash |
int |
accepts positive integers |
float |
accepts positive floating point values |
path |
like string but also accepts slashes |
uuid |
accepts UUID strings |
Convertors are used by prefixing them with a colon, like so:
@router.route('/orders/{order_id:int}')
def orders():
return 'result from the fn'
Any unknown convertor will be parsed as a regex:
@router.route('/orders/{order_id:\d{3}}')
def orders():
return 'result from the fn'
Multiple paths are supported as well:
@router.route('/', '/home')
def index():
return 'index'
Handling HTTP methods:
@router.route('/only-post', methods=['POST'])
def only_post():
return 'only-post'
Submounting routes:
subrouter = Router()
@subrouter('/items')
def items():
pass
router = Router()
router.route('/api')(subrouter)
match = router('/api/items', method='GET')
assert match, 'HTTP path is ok'
assert match.target is items
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/http-router/issues
Contributing
Development of the project happens at: https://github.com/klen/http-router
License
Licensed under a MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for http_router-2.6.3-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c73ab811ebee4f3cce4fd6e11f8016cd694177a9212339b27fa746a329cca585 |
|
MD5 | f6ed0f40e01d0072a21d52a833f31284 |
|
BLAKE2b-256 | ce178ae0c93c531765e917e1f0928270bd4405f66bddd18f2f209e8dc964a16f |
Hashes for http_router-2.6.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 963bf48adce95fbdcab76c1e8ff980910ab1ee583dae94c78dd07ccf9b0e39ad |
|
MD5 | 5785ea0697dafac7f2a073fa36a813e8 |
|
BLAKE2b-256 | 7d580e0c02cd8969cbfb28aca20fd5655cd2317ef27189b5b39b220899279237 |
Hashes for http_router-2.6.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9494c5dbc12d70081311489dbe6fce99f564f6514d688b5026e8b0f0940dd7ff |
|
MD5 | 2c2eb735bae4e1f820605b9fa4a8f36b |
|
BLAKE2b-256 | 336e954e208854adb509b15ca33871bb6b6e3e35f099f5ca99a2abcd14f895ad |
Hashes for http_router-2.6.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 304d9940ffcd8903c64893cece208d1eb08f8aecb1074a0e84643ac193dc372e |
|
MD5 | 8977228066e8d0cd606735dc1cf1a2f7 |
|
BLAKE2b-256 | 5e19bc90c2cafc629fd814a5e069d1cb813c00b027c0a91812e7fbefd3434250 |
Hashes for http_router-2.6.3-cp310-cp310-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 261a64f775de28aa20de2d16a9c292bc8c39ca8805d2331b23683ce5d60cb183 |
|
MD5 | aafa157b5b45f3e99cdb92b50c9b3126 |
|
BLAKE2b-256 | 9ccab06b63b022841d45e38d336608f6bb0e44b245153fa3b170686674379b5f |
Hashes for http_router-2.6.3-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c3a46533a696443e7f2745db2e8f073c8b058c94dd8158e1f7b4ff0f25ba210 |
|
MD5 | 2527e3e301a7c457cd549b9e5fc78f69 |
|
BLAKE2b-256 | f44ba8cf0296b97b72b730f7d996552745023e216e858662d4759241cc7f3414 |
Hashes for http_router-2.6.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e67785fbdfca0ee12e64c717ac90ea98989cd652a45dfee196b69b5484db5af6 |
|
MD5 | 809bc21f6bc121a9e6a91012b1352bc3 |
|
BLAKE2b-256 | b0ffc3487eee7700cc793ac989742df8b9ef2dfb7458ec5e7ceac0b77f527ce9 |
Hashes for http_router-2.6.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 235dfdc95e71252550b6ed38eb10575f686286260b93cb20bb9f8620742ab271 |
|
MD5 | b99a122387ca1eddd75f7c14f5855687 |
|
BLAKE2b-256 | 56c4ff849bb4638eaf909b81c01d55de780deda224c1bfd5f3a40a6ced307d82 |
Hashes for http_router-2.6.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d1bd757a2358c709cd4075010e77fae72ee7f14d5c71e3fc77626ef7585ece5 |
|
MD5 | f0491fa1f4bf97c3217e56ac4880741e |
|
BLAKE2b-256 | d278c47f90bd807792359382fbd72fc772a733ef02b0f704cd18435f1d0cc0dc |
Hashes for http_router-2.6.3-cp39-cp39-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 170eee55036e52b8a738709e7ad660ae48a36e92b73f47515eb53fc6160ed736 |
|
MD5 | ef950a7d1dd501d79a847bbfda2800ab |
|
BLAKE2b-256 | 753c1df5e176c3f0563242fb35c63cd0ebe12fe9c9c37dc0d6315392e63020d0 |
Hashes for http_router-2.6.3-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aef6d314466ad352c88199a3b8fba89c08832fd3d36e3e725fdec5ddfd77970e |
|
MD5 | 4d4e287f628b43da153dc51f8c2c847e |
|
BLAKE2b-256 | f5d47d45687f95a58fea0dbdce1442bf7efe259acc366b21e6ae22db29c52217 |
Hashes for http_router-2.6.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efdf7b0a3cd2c58fea466151a256315704d47a9b763de49e57ef79c25a8a0f3c |
|
MD5 | b481ae9be9a9d93a0c023e1735411d07 |
|
BLAKE2b-256 | 0c0f8a1772435fa3052bfcd314b1807242f6b4c95e75372439a54e0e8a040954 |
Hashes for http_router-2.6.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e21151b200d4de7e26204c60ec8a41604b54db9d4424147580a89fecd0c3ebb |
|
MD5 | 1aba8a61e72d770859ee4c3845b7532b |
|
BLAKE2b-256 | 4607d1f5b5c980c16cfe40d0b5dc59daa094c115a7a60fd73b16e8dc76332e82 |
Hashes for http_router-2.6.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8dd9bbd71474b633f5e28a1b8d0c608bb5d0c19631264dd2a5a3b8ce988dd84 |
|
MD5 | 4ccf0cb0fed286f9c94df909f365e673 |
|
BLAKE2b-256 | 0bdd58c3cadaa3445bce2f8aeaa22d1e643892bb72d81ea6a2528d319adcb25c |
Hashes for http_router-2.6.3-cp38-cp38-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ce04cd16b16cc7f340d358f86c5ba115247b6ca7dc69316a9bba20bc5ba3818 |
|
MD5 | 0d587eca37239de3212cc445163f8d6b |
|
BLAKE2b-256 | a0a8739cb88be09cd71d7d8d18e558bfd5df43563486523b9e49fd60d4057306 |
Hashes for http_router-2.6.3-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6b863662dffb37664b9a8ec393dac59f470b3fffccef0f6348eebda3927d0ed |
|
MD5 | 9d446b75ac8236cb32dd0b39653dad0e |
|
BLAKE2b-256 | 2ed481cd55c9ac1961873ff682165c00dc7480cc54fd572cc4c8cd3359cf87bd |
Hashes for http_router-2.6.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1806507f81f18e05ee638c1d5b4bca062d0124a4b9caad43cb18119fe10941af |
|
MD5 | 6c90c87c397ef3b10a4a2570de4cef88 |
|
BLAKE2b-256 | 172edbcf07a8b1e7b81097bf86c3226003a0a6bfc87c1fc80ee72924f7e8531c |
Hashes for http_router-2.6.3-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a30c4d840835da1c0163ff3892ab16cffde351356b878839439ecc1052f363fc |
|
MD5 | 56ec9a43af3138f397f8357726b15443 |
|
BLAKE2b-256 | 27a48d3b134fb543133fdde81f1e538a4973d1834a85f5e2eba4b3e2bd20793b |
Hashes for http_router-2.6.3-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa34ed5cd7dd780d36a8562cfb52bec7401019d2c45dd9943c5a58ebf067e43a |
|
MD5 | a36489174048f3a488be913985652ba8 |
|
BLAKE2b-256 | 86e54b38f0170048d756bf6f3d7153bac2c0dcbaf9f0668e8b7549e7d53faf5f |