Bottle HTTP server with dedicated server class – minimal example

This script uses a dedicated class MyServer to encapsulate the bottle server appropriately. Compared to our previous post Python bottle minimal example it provides better encapsulation of the server at the cost of slightly more lines of code.

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

class MyServer(object):
    def __init__(self): = Bottle()
    def init_routes(self):
        """Initialize all routes"""'/hello')
        def hello():
            return "Hello World!"

    def run(self):
        run(, host='', port=8080)
# Example usage
if __name__ == "__main__":
    server = MyServer()

How to use

Run the server


and open


Now you should see the Hello World! message.