Skip to main content

A very micro http framework.

Project description Join the chat at

A very micro HTTP framework. [documentation](


  • Very simple, less-code & fast
  • Using object dispatcher instead of regex route dispatcher.
  • Url-Encoded, Multipart and JSON form parsing.
  • No request and or response objects is available, everything is combined in nanohttp.context.
  • A very flexible configuration system: pymlconf
  • Method(verb) dispatcher.
  • Regex route dispatcher.
  • Powerful validation mechanism.
  • Use Python’s keywordonly arguments for query strings >= 0.24.0


The road map is to keep it simple with 100% code coverage. no built-in template engine and or ORM will be included.



$ pip install nanohttp

From Source

$ cd path/to/nanohttp
$ pip install -e .

Quick Start

from nanohttp import Controller, RestController, context, html, json, HttpFound

class TipsControllers(RestController):
    def get(self, tip_id: int = None):
        if tip_id is None:
            return [dict(id=i, title="Tip %s" % i) for i in range(1, 4)]
            return dict(
                title="Tip %s" % tip_id

    def post(self, tip_id: int = None):
        tip_title = context.form.get('title')
        print(tip_id, tip_title)

        # Updating the tips title
        # TipStore.get(tip_id).update(tip_title)
        raise HttpFound('/tips/')

class Root(Controller):
    tips = TipsControllers()

    def index(self):
        yield """
        <html><head><title>nanohttp Demo</title></head><body>
        <form method="POST" action="/tips/2">
            <input type="text" name="title" />
            <input type="submit" value="Update" />
$ nanohttp demo


from nanohttp import quickstart, configure



Do you need a WSGI application?

from nanohttp import configure, Application

configure(init_value='<yaml config string>', files=['path/to/config.file', '...'], dirs=['path/to/config/directory', '...'])
app = Application(root=Root())
# Pass the ``app`` to any ``WSGI`` server you want.

Serve it by gunicorn:

gunicorn --reload wsgi:app

Config File

Create a demo.yml file. The file below is same as the default configuration.

debug: true


  http_only: false
  secure: false

You may use nanohttp.settings anywhere to access the config values.

from nanohttp import Controller, html, settings

class Root(Controller):

    def index(self):
        yield '<html><head><title>nanohttp demo</title></head><body>'
        yield '<h2>debug flag is: %s</h2>' % settings.debug
        yield '</body></html>'

Passing the config file(s) using command line:

$ nanohttp -c demo.yml [-c another.yml] demo

Passing the config file(s) Using python:

from nanohttp import quickstart

quickstart(Root(), config='<YAML config string>')

Command Line Interface

$ nanohttp -h

usage: nanohttp [-h] [-c CONFIG_FILE] [-d CONFIG_DIRECTORY] [-b {HOST:}PORT]
                [-C DIRECTORY] [-V]

positional arguments:
                        The python module and controller class to launch.
                        default is python built-in's : `demo_app`, And the
                        default value for `:CLASS` is `:Root` if omitted.

optional arguments:
  -h, --help            show this help message and exit
  -c CONFIG_FILE, --config-file CONFIG_FILE
                        This option may be passed multiple times.
                        This option may be passed multiple times.
  -b {HOST:}PORT, --bind {HOST:}PORT
                        Bind Address. default: 8080
                        Change to this path before starting the server default
                        is: `.`
  -V, --version         Show the version.


Accessing the request cookies:

from nanohttp import context

counter = context.cookies.get('counter')

Setting cookie:

from nanohttp import context

context.cookies['dummy-cookie1'] = 'dummy-value'
context.cookies['dummy-cookie1']['http_only'] = True

For more information on how to use cookies, please check the python builtin’s http.cookies.

Trailing slashes

If the Controller.__remove_trailing_slash__ is True, then all trailing slashes are ignored.

def test_trailing_slash(self):
    self.assert_get('/users/10/jobs/', expected_response='User: 10\nAttr: jobs\n')

Decorators to indicate http handlers

Available decorators are: action, html, text, json, xml, binary

Those decorators are useful to encapsulate response preparation such as setting Content-Type HTTP header.

Take a look at the code of the action decorator, all other decorators are derived from this:

def action(*verbs, encoding='utf-8', content_type=None, inner_decorator=None):
    def _decorator(func):

        if inner_decorator is not None:
            func = inner_decorator(func)

        func.__http_methods__ = verbs if verbs else 'any'

        func.__response_encoding__ = encoding

        if content_type:
            func.__content_type__ = content_type

        return func

    if verbs and callable(verbs[0]):
        f = verbs[0]
        verbs = tuple()
        return _decorator(f)
        return _decorator

Other decorators are defined using functools.partial:

html = functools.partial(action, content_type='text/html')
text = functools.partial(action, content_type='text/plain')
json = functools.partial(action, content_type='application/json', inner_decorator=jsonify)
xml = functools.partial(action, content_type='application/xml')
binary = functools.partial(action, content_type='application/octet-stream', encoding=None)

Of-course, you can set the response content type using:

context.response_content_type = 'application/pdf'

Of-course, you can define your very own decorator to make your code DRY:

import functools
from nanohttp import action, RestController

pdf = functools.partial(action, content_type='application/pdf')

class MyController(RestController)

    def get(index):

Serving Static file(s)

The nanohttp.Static class is responsible to serve static files:

from nanohttp import Controller, Static

class Root(Controller):
    static = Static('path/to/static/directory', default_document='index.html')

Then you can access static files on /static/filename.ext

A simple way to run server and only serve static files is:

cd path/to/static/directory
nanohttp :Static

Accessing request payload

The context.form is a dictionary representing the request payload, supported request formats are query-string, multipart/form-data, application/x-www-form-urlencoded and json.

from nanohttp import context, RestController

class TipsControllers(RestController):

    def post(self, tip_id: int = None):
        tip_title = context.form.get('title')

Validating request

A decorator named: validate is available to ensure the request parameters.

from nanohttp import validate


@validate(field1=dict(required=True, min=20, max=100, type_=int, ... ))
def index(self):

A complete list of validation options is:

  • required: Boolean, indicates the field is required.
  • type_: A callable to pass the received value to it as the only argument and get it in the apprpriate type, Both ValueError and TypeError may be raised if the value cannot casted to the specified type. A good example of this callable would be the int.
  • minimum: Numeric, Minimum allowed value.
  • maximum: Numeric, Maximum allowed value.
  • pattern: Regex pattern to match the value.
  • min_length: Only for strings, the minumum allowed length of the value.
  • max_length: Only for strings, the maximum allowed length of the value.

Values for those options can be a pair of criteria, http status, for example:

    required=(True, '400 Bad Request'),
    min=(20, '471 Minimum allowed value is 20'),
    max=(100, '472 Maximum allowed value is 100'),
    type_=(int, '470 Only integers are allowed here')
def index(self):


The requested path will be split-ed by / and python’s getattr will be used on the Root controller recursively to find specific callable to handle request.

from nanohttp import RestController

class Nested(RestController):

class Root()
    children = Nested()

Then you can access methods on nested controller using: http://host:port/children

On the RestController dispatcher tries to dispatch request using HTTP method(verb) at first.


The context object is a proxy to an instance of nanohttp.Context which is unique per request.


A few hooks are available in Controller class: begin_request, begin_response, end_response.

For example this how I detect JWT token and refresh it if possible:

from nanohttp import Application, Controller, context

class JwtApplication(Application):
    token_key = 'HTTP_AUTHORIZATION'
    refresh_token_cookie_key = 'refresh-token'

    def begin_request(self):
        if self.token_key in context.environ:
            encoded_token = context.environ[self.token_key]
                context.identity = JwtPrincipal.decode(encoded_token)
            except itsdangerous.SignatureExpired as ex:
                refresh_token_encoded = context.cookies.get(self.refresh_token_cookie_key)
                if refresh_token_encoded:
                    # Extracting session_id
                    session_id = ex.payload.get('sessionId')
                    if session_id:
                        context.identity = new_token = self.refresh_jwt_token(refresh_token_encoded, session_id)
                        if new_token:
                            context.response_headers.add_header('X-New-JWT-Token', new_token.encode().decode())

            except itsdangerous.BadData:

        if not hasattr(context, 'identity'):
            context.identity = None

Rendering templates

This is how to use mako template engine with the nanohttp:

import functools
from os.path import dirname, abspath, join

from mako.lookup import TemplateLookup

from nanohttp import Controller, context, Static, settings, action

here = abspath(dirname(__file__))
lookup = TemplateLookup(directories=[join(here, 'templates')])

def render_template(func, template_name):

    def wrapper(*args, **kwargs):

        result = func(*args, **kwargs)
        if hasattr(result, 'to_dict'):
            result = result.to_dict()
        elif not isinstance(result, dict):
            raise ValueError('The result must be an instance of dict, not: %s' % type(result))

        template_ = lookup.get_template(template_name)
        return template_.render(**result)

    return wrapper

template = functools.partial(action, content_type='text/html', inner_decorator=render_template)

class Root(Controller):
    static = Static(here)

    def index(self):
        return dict(


    <title>nanohttp mako example</title>
    <h1>WSGI environ</h1>
    %for key, value in environ.items():
      <li><b>${key}:</b> ${value}</li>

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.

Files for nanohttp, version 0.29.1
Filename, size File type Python version Upload date Hashes
Filename, size nanohttp-0.29.1.tar.gz (98.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page