bottle Python HTTP custom response code minimal example

In bottle you can generate a custom HTTP response code by first importing HTTPResponse:

from bottle import HTTPResponse

and then, in your @route function, raiseHTTPResponse, for example

raise HTTPResponse("This is a HTTP 401 test URL", status=401)

Full example

#!/usr/bin/env python3
from bottle import HTTPResponse, route, run

@route('/test401')
def hello():
    raise HTTPResponse("This is a HTTP 401 test URL", status=401)

run(host='localhost', port=8080, debug=True)

Run this, script, then open http://localhost/test401 which will result in a HTTP 401 (unauthorized) response code with

This is a HTTP 401 test URL

as response body.