The Spa Web Framework

Spa is a Python micro-framework for building REST APIs and single-page-applications. It supports APIs implementing the RESTSockets pattern (streaming updates to API resources over WebSockets).


Here’s a simple Spa app:

from gevent.monkey import patch_all; patch_all()

import time
from datetime import datetime

import spa

class Hello(spa.Handler):
    def get(self, name=None):
        if name:
            return spa.Response('hello ' + name)
        return spa.Response('hello!')

    def websocket(self, name=None):
        if name:
  'hello ' + name)

        while True:
            name =
            if name:
      'hello ' + name)

class DateTime(spa.Handler):
    def get(self):
        return spa.Response(

    def websocket(self):
        while True:

routes = (
    ('/', 'home', Hello),
    ('/hello/<name>/', 'hello', Hello),
    ('/datetime/', 'datetime', DateTime),

app = spa.App(routes)

The DateTime handler demonstrates the RESTSockets pattern at work. When making an HTTP GET request, the client receives the current value. When making a WebSocket connection, the client receives a stream of values over time. You can test this locally with the wscat utility:

wscat -c ws://localhost:8000/datetime/