Skip to main content

Lightweight HTTP API server using Python stdlib

Project description

NanoServer

Zero-dependency HTTP API server built on Python's stdlib. Simple, fast, pure Python.

from nanoserver import APIServer

server = APIServer(host="0.0.0.0", port=8000)
server.add_route('GET', '/', lambda req: {"message": "Hello World"})
server.run()

No Flask. No FastAPI. No dependencies. Just Python.

Install

With pip:

pip install nanoserver

With uv:

uv add nanoserver

Quick Start

from nanoserver import APIServer

server = APIServer(port=8000)

# GET endpoint
def home(req):
    return {"message": "Hello World!"}

def hello(req):
    return {"message": "This is hello view."}

# POST endpoint
def echo(req, data):
    return {"received": data}

server.add_route('GET', '/', home)
server.add_route('GET', '/hello', hello)
server.add_route('POST', '/echo', echo)

server.run()

Test it:

curl http://localhost:8000/hello
curl -X POST http://localhost:8000/echo -d '{"test":"data"}' -H "Content-Type: application/json"

Other Usage Examples

Simple API

from nanoserver import APIServer

server = APIServer(port=8000)

def get_status(req):
    return {"status": "running", "version": "1.0"}

def create_item(req, data):
    return {"created": data, "id": 123}

server.add_route('GET', '/status', get_status)
server.add_route('POST', '/items', create_item)

server.run()

Webhook Receiver

from nanoserver import APIServer

server = APIServer(port=9000)

def handle_webhook(req, data):
    print(f"Received: {data}")
    # Process your webhook
    return {"status": "received"}

server.add_route('POST', '/webhook', handle_webhook)
server.run()

Mock API for Testing

from nanoserver import APIServer

server = APIServer(port=3000)

def get_user(req):
    return {"id": 1, "name": "John", "email": "john@example.com"}

def login(req, data):
    if data.get('password') == 'secret':
        return {"token": "abc123"}
    return {"error": "Invalid"}

server.add_route('GET', '/user', get_user)
server.add_route('POST', '/login', login)

server.run()

Inference API for Local LLM

from nanoserver import APIServer
from your_local_model_loader import model

server = APIServer(port=3000)


def inference(req, data):
    prompt = data.get('prompt')
    temperature = data.get('temperature')
    max_tokens = data.get('max_tokens')
    try:
        completion = model.create_chat_completion(
                prompt=prompt,
                temperature=temperature,
                max_tokens=max_tokens,
            )
        
        return completion
    except Exception as exc:
        raise exc

server.add_route('POST', '/inference', inference)

server.run()

Why NanoServer?

  • 🪶 Lightweight - Pure Python stdlib, no dependencies
  • ⚡ Fast - Running in miliseconds
  • 🎯 Simple - One class, two methods, done
  • 🔧 Flexible - Microservices, IoT, prototypes, webhooks

Perfect for quick APIs, prototypes, testing, and environments where you can't install heavy frameworks.

API

APIServer(host='0.0.0.0', port=8000)

Create server instance.

server.add_route(method, path, handler)

Register a route. method is 'GET' or 'POST'.

server.run()

Start the server.


Contributing

Found a bug? Want a feature? PRs welcome!

Made with ❤️ for developers who love simplicity (by Taufiq)

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

nanoserver-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

nanoserver-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file nanoserver-0.1.0.tar.gz.

File metadata

  • Download URL: nanoserver-0.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.16

File hashes

Hashes for nanoserver-0.1.0.tar.gz
Algorithm Hash digest
SHA256 150d2d8067a4fefaa7872037efc75e69111f425889fbd52930c10f6fc67c5be0
MD5 fc48e63e6506716f11e9b39e4a473d21
BLAKE2b-256 fdc782dea68a25fd7e7e9c7cc0dccdeab3fad65c517405c55aecb9f1a75d5c07

See more details on using hashes here.

File details

Details for the file nanoserver-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nanoserver-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.16

File hashes

Hashes for nanoserver-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ce3fd89a2cfcbee176f18338b2fe44b3afda5c21eec65d3aecdcb2dc568130eb
MD5 7233b352d123468a2c2dbd22280ff15c
BLAKE2b-256 e0299b03587e1166dc41396cf905f2e4769d7ed0b21feda8548e4c28fa18dcd9

See more details on using hashes here.

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