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.5-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a675ca74f3c5b2e8532d4a3a5f9441eded46313776be17595e7e53bb9c3bb2f |
|
MD5 | 0ae63f21320524087f9000abe22459e2 |
|
BLAKE2b-256 | f08e43d013df40ef222bdceae93ff7a648674bb1e1e436d3d675a7753ea48f4b |
Hashes for http_router-2.6.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e531cc214c63bb640a0cd03c73c83601d5ee69ffc64f763bd905b024b9b7c3eb |
|
MD5 | 378438a4aad0f0402e5a3a30e3947a53 |
|
BLAKE2b-256 | f3e021d74ab3fa8df7673c21da81f831fccc7024838446256d489cf368ecd883 |
Hashes for http_router-2.6.5-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c0e9e324c26ffbac83cc588a6141bca31e875ef9558a273109d4e159684b380 |
|
MD5 | bb7ff063876ab1665d24c7fdaaeedd65 |
|
BLAKE2b-256 | 9165bfd565b60e2ef2f93bf2af759f9ee8df837c0afc366439720643d42958b0 |
Hashes for http_router-2.6.5-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bf70959e6fe412b58d010af07310f4d703af27df23b2a13caaf7c69e5499d86 |
|
MD5 | 51c2b8f2d686fa889c5dbc899da47111 |
|
BLAKE2b-256 | 07137313254e31a02d6a96e7a228a7102b4ebbdfe2e4258920672d9987619f0d |
Hashes for http_router-2.6.5-cp310-cp310-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d8ba7209051c03b6e78e304774966fc0177850a0f836f307ab79174816f15a5 |
|
MD5 | d7f58ee100dd9828a85924ad910f8985 |
|
BLAKE2b-256 | 2aac3d2d209593d61dd62d50b8f727abb69344691f74c9aa8e6948d8381c2123 |
Hashes for http_router-2.6.5-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a754392dff0169489daebbc6dc521a452347dff4b56aeea631087dafd8d2223 |
|
MD5 | 15f7803733c0214c3d83079596e84c62 |
|
BLAKE2b-256 | 95d0b7f1df327ee65745a9351e4228304f68daa9417d139b0d634a0668aa1b7a |
Hashes for http_router-2.6.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 508ecb308bae9a9f42cf4a48245fae027aaf826211808c9da0fca6c851f15e3d |
|
MD5 | bda895e901709f465d46fb756770d153 |
|
BLAKE2b-256 | 4d57580c3dc03144028496e31562fa69788371f48006c0e63dc098a76b22d664 |
Hashes for http_router-2.6.5-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74e2cf082d7b3bb5c9837b0c583837e6accd77f2ec1fe69aa0a90751c546a123 |
|
MD5 | 3eea08765ecbbf6a72d38fa12f268c24 |
|
BLAKE2b-256 | e19a1ab476660f4519279b7d9c39b8a495bd33d304107189cdc328c88d53280a |
Hashes for http_router-2.6.5-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb7455d318f43abe26a525d7963cdacb9b0763b5e9fdd04054af2d2e2b60dc73 |
|
MD5 | 07fa82d490acc6e06e095c07764c7173 |
|
BLAKE2b-256 | bb9b859fb46d3fe156732b89977f0e11a4d147b2d7c2b2f9227254b462f8dfb1 |
Hashes for http_router-2.6.5-cp39-cp39-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a4cc60da8fe811efb8a5cfc0e55ee205b7a0b08c48074c6f05afc87f6bca6c0 |
|
MD5 | 8a48ee08e055573b4b8e8f42621d878b |
|
BLAKE2b-256 | 9a2cb29df170051b85b18f06bfe8df30d205fbd4695c0022ce7411178a3fd740 |
Hashes for http_router-2.6.5-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c37a415951532c3db79e69412bd290c9dafa19d2aad4180c24eaa114a45604b3 |
|
MD5 | f3d3c4bc5f35d53c80897930e678e5aa |
|
BLAKE2b-256 | 10f2738a47d793feebcd8ea755d228ee6ce88dd1176903c0e7e9170672255174 |
Hashes for http_router-2.6.5-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efe4280a999d2d7b68062a085e3d12a73b6a6551f1da546a4ac3bcdc793eae0b |
|
MD5 | 96eb41bbb7479cd4e63348cc7f425f82 |
|
BLAKE2b-256 | 490f18ba522042ed86354b436928e70b74461b3ec9c5c6157497f54adfb4d020 |
Hashes for http_router-2.6.5-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd2c513e3fd385a3b15a6ff3fd59f15890bccfa41746c5b305d898e5a66e6d0e |
|
MD5 | b9eac0c9cfdef91ca945f0fe282d257e |
|
BLAKE2b-256 | 8e24510aa49a84f579b86d436e25b3bdce6c4ced539a33d1f829a8984db05127 |
Hashes for http_router-2.6.5-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd1a72901b30dd65b72556e291b992735784730e8802b0e943e0aafea110bf80 |
|
MD5 | ec1577c7690265cd112d69ef0ee01ffc |
|
BLAKE2b-256 | 7da10daba865f1777d718e825867d5d670f33be18f493a6fe3d42c260684f30b |
Hashes for http_router-2.6.5-cp38-cp38-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a03ad3addba310f9ea5a0a52fb852e27a19dabff95419e1661a93b56d70c0d51 |
|
MD5 | b87452b17b35d9967b1023e53900e2d5 |
|
BLAKE2b-256 | cbcd87ce71d8131cc8aa3b49e9c608410c3168b2010348929c19045f848d2a91 |
Hashes for http_router-2.6.5-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb6cc3da118eb4a41e566632d5dc5860fbbd9b58bf836fb1c9ceec19fffa35fa |
|
MD5 | 0ac597f1de5e8b3915ec53200d835610 |
|
BLAKE2b-256 | cdd731945133e1c991f03e3e76327cce73a6b98365b737f2a55bea4dcfeb09e5 |
Hashes for http_router-2.6.5-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 814694ca90adb79793ae8822d5ab6bf39631f3e35526dafd1f54c8ecfa26c54f |
|
MD5 | 1b6d2d8fc6ddc06e6a59987581ac5e9c |
|
BLAKE2b-256 | c2b3f1e7490b3da2636606c2ebdfcdf8f94df61cf65844bbef7a8e99db53807c |
Hashes for http_router-2.6.5-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91d6615d25d28420d8e701d14e50fc8c5e2e6ca05dc609f896852304f49f3dac |
|
MD5 | ab0b413b33619b479689a3a954f4658c |
|
BLAKE2b-256 | e98dd5470b212eccfd139bce8514dc4ded69bb9f6ad303a4640cf669c3f2b508 |
Hashes for http_router-2.6.5-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0eb96e9148ab1500f825e6f6b40c20571dcbb7ac7846e52414305b24fb9f6779 |
|
MD5 | d7fbee925250af61c5cde6f82f0c4c36 |
|
BLAKE2b-256 | 422f10916056bae58f155e614ef00fac3263249fbade6b6da144cdd4d670df82 |