Skip to main content

Small, fast, HTTP URL router

Project description

GitHub Actions Workflow Status

nanoroute

A small Python HTTP URL routing facility capable of sub-microsecond routing, typically <200ns.

Nanoroute is a C++ implementation of a modified version of the priority radix-tree algorithm pioneered by Julien Schmidt's httprouter. Like httprouter, nanorouter's underlying implementation performs no allocations in the common case, although some allocations are necessary to create the PyObjects necessary for the CPython bindings.

The nanoroute package is suitable as a building block for more fully featured HTTP frameworks. It also provides a simple WSGI interface for integration directly into WSGI application stacks.

The intended use cases are high-performance message brokers, dispatchers, and ingestion endpoints. The performance improvements of a high-speed router are unlikely to be very significant in a typical database-backed Python REST API.

Quickstart

For complete documentation, see the docs.

Installation

Nanoroute only supports Python 3.13+. It is available via PyPI, any mechanism of installing Python packages from PyPI will work:

pip install nanoroute

Registering Routes

Nanoroute provides a single class, the router, which can be used to register handlers for common HTTP verbs.

import nanoroute

app = nanoroute.router()

@app.get('/')
def root(*args, **kwargs):
  ...

@app.post('/endpoint')
def endpoint(*args, **kwargs):
  ...

The verbs supported via these convenience methods are GET, POST, PUT, and DELETE. Arbitrary sets of any valid HTTP verbs can be registered using router.route().

# Register for a single HTTP verb
@app.route('PATCH', '/object')
def handle_patch(*args, **kwargs):
  ...

# Register for a multiple HTTP verbs
@app.route(['POST', 'PUT'], '/multi-meth')
def handle_multi(*args, **kwargs):
  ...

Finally, any arbitrary object can be registered with nanoroute. The decorator syntax is merely convenient for typical usage.

# Register arbitrary object for GET '/'
app.get('/')(object())

Capturing Parameters

Two forms of parameter capture are available, segment capture and catchall. Segment captures are delimited by : and capture from the appearance of the delimiter until the following / or the end of the URL. Catchalls are delimited by * and capture the entire URL following their appearance.

Both types of parameter capture may be followed by a name, which will used as the key associated with the parameter during route lookup. Anonymous parameters act as wildcards, they have the same behavior as named parameters but the captured data is not reported during lookup.

# Captures the middle segment with the name "id"
@app.get('/user/:id/profile')
def get_profile(*args, **kwargs):
  ...

# Captures the article ID into "id", and then everything after the final "/"
# is captured as "slug" which might contain multiple path segments
@app.get('/article/:id/*slug')
def article(*args, **kwargs):
  ...

# The first path segment is a wildcard, anything may appear, but nothing is
# captured during route lookup
@app.get('/:/anonymous')
def anon(*args, **kwargs):
  ...

Captures are allowed to appear at arbtirary points in a given segment, so long as multiple captures do not appear in the same segment.

# Captures an "id" in the middle of a segment
@app.get('/user_:id/profile')
def get_profile(*args, **kwargs):
  ...

# Error: Invalid wildcard construction. Only one capture is allowed to appear
# in a given path segment
@app.get('/user_:id_:name')
def this_is_an_error(*args, **kwargs):
  ...

Captures that appear in the same place for different paths may have different names, which will be recorded appropriately.

# Captures the first segment as "foo"
@app.get('/:foo/alpha')
def alpha(*args, **kwargs):
  ...

# Captures the first segment as "bar"
@app.get('/:bar/beta')
def beta(*args, **kwargs):
  ...

Lookup

The base lookup facility is router.lookup(), which is intended for other frameworks to use as a building block. It takes a method and path as arguments and returns the registered handler and a parameter capture dictionary.

@app.get('/user/:name')
def say_hello(params):
  print(f'Hello {params['name']}!')

handler, params = app.lookup('GET', '/user/Jane')
handler(params)

# >>> Hello Jane!

As a convenience, a WSGI application is also provided. It is directly equivalent to the following code:

def wsgi_app(environ, start_response):
  handler, params = app.lookup(environ['REQUEST_METHOD'], environ['PATH_INFO'])
  environ['nanoroute.params'] = params
  return handler(environ, start_response)

app.wsgi_app = wsgi_app

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nanoroute-2.0.5.tar.gz (19.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

nanoroute-2.0.5-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (35.0 kB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

nanoroute-2.0.5-cp314-cp314t-macosx_13_0_arm64.whl (25.3 kB view details)

Uploaded CPython 3.14tmacOS 13.0+ ARM64

nanoroute-2.0.5-cp314-cp314-win_amd64.whl (85.2 kB view details)

Uploaded CPython 3.14Windows x86-64

nanoroute-2.0.5-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (34.2 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

nanoroute-2.0.5-cp314-cp314-macosx_13_0_arm64.whl (24.8 kB view details)

Uploaded CPython 3.14macOS 13.0+ ARM64

nanoroute-2.0.5-cp313-cp313-win_amd64.whl (82.9 kB view details)

Uploaded CPython 3.13Windows x86-64

nanoroute-2.0.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (34.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

nanoroute-2.0.5-cp313-cp313-macosx_13_0_arm64.whl (24.8 kB view details)

Uploaded CPython 3.13macOS 13.0+ ARM64

File details

Details for the file nanoroute-2.0.5.tar.gz.

File metadata

  • Download URL: nanoroute-2.0.5.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nanoroute-2.0.5.tar.gz
Algorithm Hash digest
SHA256 9a8eacc53fdef3d52e6ead856d4fe4dd04a7b7f0d3c0d08fdae50bdddb42abd3
MD5 71e928af651a20c5261fdd464c168bd3
BLAKE2b-256 fe997bd335b4badbfad14a8b51c653306ad6a5b0ef879ec68ed643da56c0a38d

See more details on using hashes here.

Provenance

The following attestation bundles were made for nanoroute-2.0.5.tar.gz:

Publisher: publish.yaml on nickelpro/nanoroute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nanoroute-2.0.5-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nanoroute-2.0.5-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 204339702995486078f2a770022aaee84a32f2ccebc1f669dadd835ad2ed0f53
MD5 23ff84503f7ff06b6fe9d00bf5fd2f1b
BLAKE2b-256 a021c84a15f323f4464765691cb870f911b278d5c59304f299407f79244358fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for nanoroute-2.0.5-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish.yaml on nickelpro/nanoroute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nanoroute-2.0.5-cp314-cp314t-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for nanoroute-2.0.5-cp314-cp314t-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 76e5ac7eb1bed68e2fae8ae64ceda87a903de693a3d6a962ba922de6719c345b
MD5 aba43d2377b9ff06e029491a69f0c613
BLAKE2b-256 56785fe2a6fd03cf73d7f994e2011ccf9b736d9c62e5f3d291dcee00aec783f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for nanoroute-2.0.5-cp314-cp314t-macosx_13_0_arm64.whl:

Publisher: publish.yaml on nickelpro/nanoroute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nanoroute-2.0.5-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: nanoroute-2.0.5-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 85.2 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nanoroute-2.0.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 0140e198a1de8256a2bbbda34208d52bc7a9be64d67ae931ce459e07e2767b74
MD5 b028e9b1ef7c9d5460989ed930a4df7a
BLAKE2b-256 a5784b8e32fd40a383fe3257555cf4eac47703572b51039f4f682bd6e8859401

See more details on using hashes here.

Provenance

The following attestation bundles were made for nanoroute-2.0.5-cp314-cp314-win_amd64.whl:

Publisher: publish.yaml on nickelpro/nanoroute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nanoroute-2.0.5-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nanoroute-2.0.5-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b7e0b6629c90cae2f08ec044ad9f81a93fe7f60a856305ee9b59cf98564fd33e
MD5 df4fccf0d6c0c9bce18d8c1ea0f6d3a2
BLAKE2b-256 0d29ed85faffc8486e7d9f2ce051224960694cc4e2bc2fc1e6bf671c249a6ee4

See more details on using hashes here.

Provenance

The following attestation bundles were made for nanoroute-2.0.5-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish.yaml on nickelpro/nanoroute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nanoroute-2.0.5-cp314-cp314-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for nanoroute-2.0.5-cp314-cp314-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 8194f8466348f33ac2f73a817090a8aefa7670a4a23e323d82a703e8e1444d3c
MD5 d06cafa6edae794954e5a47f9856dc94
BLAKE2b-256 147c7d243b75648075284f8a280ec2f5fc23468e52d4ca28baa1fb1c0cc03ee6

See more details on using hashes here.

Provenance

The following attestation bundles were made for nanoroute-2.0.5-cp314-cp314-macosx_13_0_arm64.whl:

Publisher: publish.yaml on nickelpro/nanoroute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nanoroute-2.0.5-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: nanoroute-2.0.5-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 82.9 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nanoroute-2.0.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 257f34b9a5408c0190310600cb7278138a7f35513d97475446ebfe7aaf428159
MD5 2268bc38e5b8176f941b5340e55d1e33
BLAKE2b-256 4f35141e02b626e24bb7de4bb4bcc53752c4c78b4444bd69b66098303a10200a

See more details on using hashes here.

Provenance

The following attestation bundles were made for nanoroute-2.0.5-cp313-cp313-win_amd64.whl:

Publisher: publish.yaml on nickelpro/nanoroute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nanoroute-2.0.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nanoroute-2.0.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 733480cf7b57a214665371ffabdca7d572ea6cb2d7eb313840a08054f5929a26
MD5 488938b9223dd51f13e663c4a4499db8
BLAKE2b-256 ced7b0f7089ed371f2d5ca02e9329600a252945fb604a37fc3f449d60ada4c16

See more details on using hashes here.

Provenance

The following attestation bundles were made for nanoroute-2.0.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish.yaml on nickelpro/nanoroute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nanoroute-2.0.5-cp313-cp313-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for nanoroute-2.0.5-cp313-cp313-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 56889e99d079875883cab81e3e209411a8a1f2465777829ddbc1da6c47e0c68a
MD5 4dc0081b6cfee7156bc86ad9c3924e67
BLAKE2b-256 c287ebf035ed63774517660e0dd55a440cdaf4f3aa6b69985135ab14900be019

See more details on using hashes here.

Provenance

The following attestation bundles were made for nanoroute-2.0.5-cp313-cp313-macosx_13_0_arm64.whl:

Publisher: publish.yaml on nickelpro/nanoroute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page