skip to navigation
skip to content

skitai 0.26b31

Skitai App Engine For Microservices

Latest Version: 0.26.18b45

Table of Contents


Skitai is a kind of branch of Medusa Web Server - A High-Performance Internet Server Architecture.

Medusa is different from most other servers because it runs as a single process, multiplexing I/O with its various client and server connections within a single process/thread.

Skitai orients light-weight, simplicity and strengthen networking operations with external resources - HTTP / HTTPS / XML-RPC / PostgreSQL - keeping very low costs. And it is being optimized for building microservices.

  • Working as Web, XML-RPC and Reverse Proxy Loadbancing Server
  • HTML5 Websocket & HTTP/2.0 implemeted
  • Handling massive RESTful API/RPC/HTTP(S) connections based on asynchronous socket framework at your apps easily
  • Asynchronous connection pool with PostgreSQL, MongoDB and Redis

Skitai is not a framework for convinient developing, module reusability and plugin flexibility etc. It just provides some powerful communicating services for your WSGI apps as both server and client.

Also note it is inspired by Zope and Flask a lot.

Conceptually, Skitai has been seperated into two components:

  1. Skitai App Engine Server, for WSGI apps
  2. Skito-Saddle, the small WSGI container integrated with Skitai. But you can also mount any WSGI apps and frameworks like Flask.



On win32, required pywin32 binary


pip install skitai

Another way from Git:

git clone
cd skitai
python install

But generally you don’t need install alone. When you install Skitai App Engine, proper version of Skitai App Engine will be installed.

Starting Skitai

Basic Usage


def app (env, start_response):
  start_response ("200 OK", [("Content-Type", "text/plain")])
  return ['Hello World']

app.use_reloader = True
app.debug = True

if __name__ == "__main__":

  import skitai

  skitai.mount ('/', app) (
      address = "",
      port = 5000

At now, run this code from console.


You can access this WSGI app by visiting

If you want to allow access to your public IPs, or specify port:

skitai.mount ('/', app) (
  address = "",
  port = 5000

skital.mount () spec is:

mount (mount_point, mount_object, app_name = “app”, pref = None)

  • mount_point

  • mount_object: app, app file path or module object

    skitai.mount ('/', app)
    skitai.mount ('/', 'app_v1/', 'app')
    import wissen
    skitai.mount ('/', wissen, 'app')
    skitai.mount ('/', (wissen, ''), 'app')

    In case module object, the module should support skitai exporting spec.

  • app_name: variable name of app

  • pref: see nect section

Runtime App Preference

Usally, your app preference setting is like this:

from flask import Flask
app = Flask(__name__)

app.use_reloader = True
app.debug = True
app.config ["prefA"] = 1
app.config ["prefB"] = 2

Skitai provide runtime preference setting.

import skitai

pref = skitai.apppref ()
pref.use_reloader = 1
pref.debug = 1

pref.config ["prefA"] = 1
pref.config.prefB = 2

skitai.mount ("/v1", "app_v1", "app", pref) ()

Above pref’s all properties will be overriden on your app.

Logging and Console Displaying

If you do not specify log file path, all logs will be displayed in console, bu specifed all logs will be written into file.

skitai.mount ('/', app) (
  address = "",
  port = 5000,
  logpath = '/var/logs/skitai'

If you also want to view logs through console for spot developing, you run with-v option.

python3 -v

Run As Daemon

python3 start


python3 -d

For stopping daemon,

python3 stop

Run with Threads Pool

Skitai run defaultly multi-threading mode and number of threads are 4. If you want to change number of threads for handling WSGI app:

skitai.mount ('/', app) (
  threads = 8

Run with Single-Thread

If you want to run Skitai with entirely single thread,

skitai.mount ('/', app) (
  threads = 0

This features is limited by your WSGI container. If you use Skito-Saddle container, you can run with single threading mode by using Skito-Saddle’s async streaming response method. But you don’t and if you have plan to use Skitai ‘was’ requests services, you can’t single threading mode and you SHOULD run with multi-threading mode.

Mount Multiple WSGI Apps And Static Directories

Here’s three WSGI app samples:


def app1 (env, start_response):
  start_response ("200 OK", [("Content-Type", "text/plain")])
  return ['Hello World']

app1.use_reloader = True
app1.debug = True

# Flask App*
from flask import Flask
app2 = Flask(__name__)

app2.use_reloader = True
app2.debug = True

def index ():
  return "Hello World"

# Skito-Saddle App
from skitai.saddle import Saddle
app3 = Saddle (__name__)

app3.use_reloader = True
app3.debug = True

def index (was):
  return "Hello World"

Then place this code at bottom of above WSGI app.

if __name__ == "__main__":

  import skitai

  skitai.mount ('/t1', __file__, 'app1')
  skitai.mount ('/t2', __file__, 'app2')
  skitai.mount ('/t3', __file__, 'app3')
  skitai.mount ('/', 'static') ()

These feaure can used for managing version.

Let’s assume initail version of app file is

app = Saddle (__name__)

def index (was):
  return "Hello World Ver.1"

And in same directory 2nd version of app file is

app = Saddle (__name__)

def index (was):
  return "Hello World Ver.2"

Now is like this:

import skitai

skitai.mount ('/', 'static')
skitai.mount ('/v1', 'app_v1')
skitai.mount ('/v2', 'app_v2') ()

Then run with:


You can access ver.1 by and vwe.2 by

Note: Above 3 files is in the same directory and then both share templates directory. If you intend to seperate from app_v1 and app_v2, you should seperate app with directory like this:



and your

import skitai

skitai.mount ('/v1', 'app_v1/static'),
skitai.mount ('/v1', 'app_v1/app'),
skitai.mount ('/v2', 'app_v2/static'),
skitai.mount ('/v2', 'app_v2/app') ()

Mounting With Virtual Host

if __name__ == "__main__":

  import skitai
  skitai.vmount ('', '/', '')
  skitai.vmount ('', '/', '') ()

Enabling Proxy Server

skitai.enable_proxy ()
skitai.mount ('/', app) ()

Adding Server(s) Alias

Cluster should be defined like this: (alias_type, servers, role = “”, source = “”, ssl = False).

  • alias_type: available database or protocol types are:
    • PROTO_WS: websocket
    • PROTO_WSS: SSL websocket
    • DB_PGSQL
    • DB_SQLITE3
    • DB_REDIS
  • server: single or server list
  • role (optional): it is valid only when cluster_type is http or https for controlling API access
  • source (optional): comma seperated ipv4/mask
  • ssl (optional): use SSL connection or not, PROTO_HTTPS and PROTO_WSS use SSL defaultly
skitai.mount ('/', app)
skitai.alias (
  '@members', skitai.PROTO_HTTP, "",
  role = 'admin', source = ',192.168.1/24'
skitai.alias (
  '@mysqlite3', skitai.DB_SQLITE3, ["/var/tmp/db1", "/var/tmp/db2"]
) ()

Run as HTTPS Server

To genrate self-signed certification file:

openssl req -new -newkey rsa:2048 -x509 -keyout server.pem -out server.pem -days 365 -nodes
skitai.mount ('/', app)
skitai.enable_ssl ('server.pem', 'key.pem', 'your pass phrase') ()

About Mount Point & App Routing

If app is mounted to ‘/flaskapp’,

from flask import Flask
app = Flask (__name__)

@app.route ("/hello")
def hello ():
  return "Hello"

Above /hello can called,

Also app should can handle mount point. In case Flask, it seems ‘url_for’ generate url by joining with env[“SCRIPT_NAME”] and route point, so it’s not problem. Skito-Saddle can handle obiously. But I don’t know other WSGI containers will work properly.

SMTP Delivery Agent

New in version 0.26

e-Mail sending service is executed seperated system process not threading. Every e-mail is temporary save to file system, e-Mail delivery process check new mail and will send. So there’s possibly some delay time.

You can send e-Mail in your app like this:

# email delivery service
e = (subject, snd, rcpt)
e.set_smtp ("", "username", "password", ssl = True)
e.add_content ("Hello World<div><img src='cid:ID_A'></div>", "text/html")
e.add_attachment (r"001.png", cid="ID_A")
e.send ()

With asynchronous email delivery service, can add default SMTP Server. If it is configured, you can skip e.set_smtp(). But be careful for keeping your smtp password.

skitai.enable_smtpda (
  '', 'user', 'password',
  ssl = False, max_retry = 10, keep_days = 3
skitai.mount ('/', app) ()

All e-mails are saved into varpath and varpath is not specified default is /var/temp/skitai

Batch Task Scheduler

New in version 0.26

Sometimes app need batch tasks for minimum response time to clients. At this situateion, you can use taks scheduling tool of OS - cron, taks scheduler - or can use Skitai’s batch task scheduling service for consistent app management.

skitai.cron ("*/2 */2 * * *", "/home/apps/  > /home/apps/monitor.log 2>&1")
skitai.cron ("9 2/12 * * *", "/home/apps/ > /dev/null 2>&1")
skitai.mount ('/', app) ()

Taks configuarion is very same with posix crontab.

Asccessing File Reources On Startup

Skitai’s working directory is where the script call (). Even you run skitai at root directory,

/app/example/ -d

Skitai will change working directory to /app/example on startup.

So your file resources exist within skitai run script, you can access them by relative path,

monitor = skital.joinpath ('appack', '')
skitai.cron ("*/2 */2 * * *", "%s > /home/apps/monitor.log 2>&1" % monitor)

Also, you need absolute path on script,

skitai.getswd () # get skitai working directory

Enable Cache File System

If you make massive HTTP requests, you can cache contents by HTTP headers - Cache-Control and Expires

skitai.enable_cachefs (path = '/var/skitai/cache', memmax = 0, diskmax = 0)
skitai.mount ('/', app) ()

Configure Max Age For Static Files

You can set max-age for static files’ respone header like,

..code:: bash

Cache-Control: max-age=300 Expires: Sun, 06 Nov 2017 08:49:37 GMT

If max-age is only set to “/”, applied to all files. But you can specify it to any sub directories.

skitai.mount ('/', 'static')
skitai.set_max_age ("/", 300)
skitai.set_max_age ('/js', 0)
skitai.set_max_age ('/images', 3600) ()

Enable Cache File System

If you make massive HTTP requests, you can cache contents by HTTP headers - Cache-Control and Expires

skitai.enable_cachefs (path = '/var/skitai/cache', memmax = 0, diskmax = 0)
skitai.mount ('/', app) ()

Enabling API Gateway Server

Using Skitai’s reverse proxy feature, it can be used as API Gateway Server. All backend API servers can be mounted at gateway server with client authentification and transaction ID logging feature.

class Authorizer:
  def __init__ (self):
    self.tokens = {
      "12345678-1234-123456": ("hansroh", ["user", "admin"], 0)

  # For Token
  def handle_token (self, handler, request):
    username, roles, expires = self.tokens.get (request.token)
    if expires and expires < time.time ():
      # remove expired token
      self.tokens.popitem (request.token)
      return handler.continue_request (request)
    handler.continue_request (request, username, roles)

  # For JWT Claim
  def handle_claim (self, handler, request):
    claim = request.claim
    expires = claim.get ("expires", 0)
    if expires and expires < time.time ():
      return handler.continue_request (request)
    handler.continue_request (request, claim.get ("user"), claim.get ("roles"))

def startup (wasc):
  wasc.handler.set_auth_handler (Authorizer ())

@app.route ("/")
def index (was):
  return "<h1>Skitai App Engine: API Gateway</h1>"

if __name__ == "__main__":
  import skitai

  skitai.alias (
    '@members', 'https', "",
    role = 'admin', source = ',192.168.1/24'
  skitai.alias (
    '@photos', skitai.DB_SQLITE3, ["/var/tmp/db1", "/var/tmp/db2"]
  skitai.mount ('/', app)
  skitai.mount ('/members', '@members')
  skitai.mount ('/photos', '@photos')
  skitai.enable_gateway (True, "8fa06210-e109-11e6-934f-001b216d6e71") ()

Gateway use only bearer tokens like OAuth2 and JWT(Json Web Token) for authorization. And token issuance is at your own hands. But JWT creation,

from aquests.lib import jwt

secret_key = b"8fa06210-e109-11e6-934f-001b216d6e71"
token = jwt.gen_token (secret_key, {'user': 'Hans Roh', 'roles': ['user']}, "HS256")

Also Skitai create API Transaction ID for each API call, and this will eb explained in Skitai ‘was’ Service chapter.

Using Database Engine For Verifying Token

New in version 0.24.8

If you are not familar with Skitai ‘was’ request services, it would be better to skip and read later.

You can query for getting user information to database engines asynchronously. Here’s example for MongDB.

from skitai import was

class Authorizer:
  def handle_user (self, response, handler, request):
    username = ['username']
    roles = ['roles']
    expires = ['expires']

    if expires and expires < time.time ():
      was.mongodb (
        "@my-mongodb", "mydb", callback = lambda x: None,
      ).delete ('tokens', {"token": request.token})
      handler.continue_request (request)
      handler.continue_request (request, username, roles)

  def handle_token (self, handler, request):
    was.mongodb (
      "@my-mongodb", "mydb", callback = (self.handle_user, (handler, request))
    ).findone ('tokens', {"token": request.token})

Skitai with Nginx / Squid

From version 0.10.5, Skitai supports virtual hosting itself, but there’re so many other reasons using with reverse proxy servers.

Here’s some helpful sample works for virtual hosting using Nginx / Squid.

If you want 2 different and totaly unrelated websites:


Then you can reverse proxying using Nginx, Squid or many others.

Example Squid config file (squid.conf) is like this:

http_port 80 accel

cache_peer parent 5000 0 no-query originserver name=jeans
acl jeans-domain dstdomain
http_access allow jeans-domain
cache_peer_access jeans allow jeans-domain
cache_peer_access jeans deny all

cache_peer parent 5001 0 no-query originserver name=carsales
acl carsales-domain dstdomain
http_access allow carsales-domain
cache_peer_access carsales allow carsales-domain
cache_peer_access carsales deny all

For Nginx might be 2 config files (I’m not sure):

; /etc/nginx/sites-enabled/
server {
        listen 80;
  location / {

; /etc/nginx/sites-enabled/
server {
        listen 80;
  location / {

Export API From Your Module Through Skitai

If your module need export API, include app in your module for Skitai.

Let’s assume your package name is ‘unsub’.

Your app should be located at unsub/export/skitai/

Then users using your module can mount on skitai,

import unsub

pref = skitai.pref ()
pref.config.urlfile = skitai.joinpath ('resources', 'urllist.txt')
skitai.mount ("/v1", (unsub, "app_v1"), "app", pref) ()

If your app need to complicated initialize process from simple options, write code to unsub/export/skitai/

import skitai
from . import cronjob

def init_app (pref):
  skitai.cron ('*/10 * * * *', cronjob.__file__)

  with open (pref.config.urlfile, "r") as f:
    pref.config.urllist = []
    while 1:
      line = f.readline ().strip ()
      if not line: break
      pref.config.urllist.append (line.split ("\t", 4))


Wissen RESTful API is an WSGI implementation for Wissen with Skitai App Engine.

Skitai ‘was’ Services

‘was’ means (Skitai) WSGI Application Support.

WSGI container like Flask, need to import ‘was’:

from skitai import was

@app.route ("/")
def hello ():
  was.get ("http://...")

But Saddle WSGI container integrated with Skitai, use just like Python ‘self’.

It will be easy to understand think like that:

  • Skitai is Python class instance
  • ‘was’ is ‘self’ which first argument of instance method
  • Your app functions are methods of Skitai instance
@app.route ("/")
def hello (was, name = "Hans Roh"):
  was.get ("http://...")

Simply just remember, if you use WSGI container like Flask, Bottle, … - NOT Saddle - and want to use Skitai asynchronous services, you should import ‘was’. Usage is exactly same. But for my convinient, I wrote example codes Saddle version mostly.

Async Requests Service

Most importance service of ‘was’ is making requests to HTTP, REST, RPC and Database Engines. The modules related theses features from aquests.

You can read aquests usage first.

I think it just fine explains some differences with aquests.

First of all, usage is somewhat different because aquests is used within threadings on skitai. Skitai takes some threading advantages and compromise with them for avoiding callback heaven.


At aquests,

import aquests

def display_result (response):
  print (

aquests.configure (callback = display_result, timeout = 3)

aquests.get (url) (url, {"user": "Hans Roh", "comment": "Hello"})
aquests.fetchall ()

At Skitai,

def request (was):
  req1 = was.get (url)
  req2 = (url, {"user": "Hans Roh", "comment": "Hello"})
  respones1 = req1.getwait (timeout = 3)
  response2 = req2.getwait (timeout = 3)
  return [,]

The significant differnce is calling getwait (timeout) for getting response data.

PostgreSQL query at aquests,

import aquests

def display_result (response):
  for row in, row.t_high, row.t_low

aquests.configure (callback = display_result, timeout = 3)

dbo = aquests.postgresql ("", "mydb")
dbo.excute ("SELECT city, t_high, t_low FROM weather;")
aquests.fetchall ()

At Skitai,

def query (was):
  dbo = was.postgresql ("", "mydb")
  s = dbo.excute ("SELECT city, t_high, t_low FROM weather;")

  response = s.getwait (2)
  for row in, row.t_high, row.t_low

If you needn’t returned data and just wait for completing query,

dbo = was.postgresql ("", "mydb")
req = dbo.execute ("INSERT INTO CITIES VALUES ('New York');")
req.wait (2)

If failed, exception will be raised.

Here’re addtional methods and properties above response obkect compared with aquests’ response one.

  • cache (timeout): response caching
  • status: it indicate requests processed status and note it is not related response.status_code.
    • 0: Initial Default Value
    • 1: Operation Timeout
    • 2: Exception Occured
    • 3: Normal Terminated

Usage At Single Threaded Environment

If you run Skitai with single threaded mode, you can’t use req.wait(), req.getwait() or req.getswait(). Instead you should use callback for this, and Skitai provide async response.

def response_handler (response, proxy):
  proxy.done (response.content)

@app.route ("/index")
def aresponse_example (was):
  proxy = was.aresponse (response_handler)
  proxy.get (None, "")
  return proxy

Unfortunately this feature is available on Skito-Saddle WSGI container only (It means Flask or other WSGI container users can only use Skitai with multi-threading mode).

For more detail usage will be explained ‘Skito-Saddle Async Streaming Response’ chapter and you could skip now.


Skitai support load-balancing requests.

If server members are pre defined, skitai choose one automatically per each request supporting fail-over.

Then let’s request XMLRPC result to one of mysearch members.

@app.route ("/search")
def search (was, keyword = "Mozart"):
  s = ("@mysearch/rpc2").search (keyword)
  results = s.getwait (5)

if __name__ == "__main__":
  import skitai (
    clusters = {
      ('https', ["", ""])
    mount = ("/", app)

It just small change from was.rpc () to ()

Note: If @mysearch member is only one, (“@mydb”) is equal to was.get (“@mydb”).

Note2: You can mount cluster @mysearch to specific path as proxypass like this:

if __name__ == "__main__":
  import skitai (
    clusters = {
      ('https', ["", ""])
    mount = [
      ("/", app),
      ("/search", '@mysearch')

It can be accessed from, and handled as load-balanced proxypass.

This sample is to show loadbalanced querying database. Add mydb members to config file.

@app.route ("/query")
def query (was, keyword):
  dbo = ("@mydb")
  req = dbo.execute ("SELECT * FROM CITIES;")
  result = req.getwait (2)

 if __name__ == "__main__":
  import skitai (
    clusters = {
    mount = [
      ("/", app)


Basically same with load_balancing except Skitai requests to all members per each request.

@app.route ("/search")
def search (was, keyword = "Mozart"):
  stub = ("@mysearch/rpc2")
  req = (keyword)
  results = req.getswait (2)

  all_results = []
  for result in results:
     all_results.extend (
  return all_results

There are 2 changes:

  1. from () to ()
  2. from s.getwait () to s.getswait () for multiple results, and results is iterable.

Caching Result

Every results returned by getwait(), getswait() can cache.

s = ("@mysearch/rpc2").getinfo ()
result = s.getwait (2)
if result.status_code == 200:
      result.cache (60) # 60 seconds

s = ("@mysearch/rpc2").getinfo ()
results = s.getswait (2)
# assume @mysearch has 3 members
if results.status_code == [200, 200, 200]:
  result.cache (60)

Although code == 200 alredy implies status == 3, anyway if status is not 3, cache() will be ignored. If cached, it wil return cached result for 60 seconds.

New in version 0.15.28

If you getwait with reraise argument, code can be simple.

s = ("@mysearch/rpc2").getinfo ()
content = s.getswait (2, reraise = True).data
s.cache (60)

Please note cache () method is both available request and result objects.

You can control number of caches by your system memory before running app.

skitai.set_max_rcache (300)
skitai.mount ('/', app) ()

New in version 0.14.9

For expiring cached result by updating new data:

refreshed = False
if was.request.command == "post":
  refreshed = True

s = (
      use_cache = not refreshed and True or False
).getinfo ()
result = s.getwait (2)
if result.status_code == 200:
      result.cache (60) # 60 seconds

API Transaction ID

New in version 0.21

For tracing REST API call, Skitai use global/local transaction IDs.

If a client call a API first, global transaction ID (gtxnid) is assigned automatically like ‘GTID-C4676-R67’ and local transaction ID (ltxnid) is ‘1000’.

You call was.get (), () or etc, both IDs will be forwarded via HTTP request header. Most important thinng is that gtxnid is never changed by client call, but ltxnid will be changed per API call.

when client calls gateway API or HTML, ltxnid is 1000. And if it calls APIs internally, ltxnid will increase to 2001, 2002. If ltxnid 2001 API calls internal sub API, ltxnid will increase to 3002, and ltxnid 2002 to 3003. Briefly 1st digit is call depth and rest digits are sequence of API calls.

This IDs is logged to Skitai request log file like this.

2016.12.30 18:05:06 [info] GET / \
HTTP/1.1 200 0 32970 \
GTID-C3-R8 1000 - - \
"Mozilla/5.0 (Windows NT 6.1;) Gecko/20100101 Firefox/50.0" \
4ms 3ms

Focus 3rd line above log message. Then you can trace a series of API calls from each Skitai instance’s log files for finding some kind of problems.

In next chapters’ features of ‘was’ are only available for Skito-Saddle WSGI container. So if you have no plan to use Saddle, just skip.

Utility Methods of ‘was’

This chapter’s ‘was’ services are also avaliable for all WSGI middelwares.

  • was.status () # HTML formatted status information like phpinfo() in PHP.
  • was.tojson (object)
  • was.fromjson (string)
  • was.toxml (object, usedatetime = 0) # XMLRPC
  • was.fromxml (string) # XMLRPC
  • was.togrpc (object) # gRPC
  • was.fromgrpc (message, obj) # gRPC
  • was.restart () # Restart Skitai App Engine Server, but this only works when processes is 1 else just applied to current worker process.
  • was.shutdown () # Shutdown Skitai App Engine Server, but this only works when processes is 1 else just applied to current worker process.

HTML5 Websocket

New in version 0.11

The HTML5 WebSockets specification defines an API that enables web pages to use the WebSockets protocol for two-way communication with a remote host.

Skitai can be HTML5 websocket server and any WSGI containers can use it.

But I’m not sure my implemetation is right way, so it is experimental and could be changable.

First of all, see conceptual client side java script for websocket.

<ul id="display"></ul>
<input id="mymsg" type="text">
<button onclick='talk ();'>Submit<button>

<script language="javascript" type="text/javascript">
var wsUri = "ws://localhost:5000/websocket/chat";

function testWebSocket()
  websocket = new WebSocket(wsUri);
  websocket.onopen = function(evt) { onOpen(evt) };
  websocket.onclose = function(evt) { onClose(evt) };
  websocket.onmessage = function(evt) { onMessage(evt) };
  websocket.onerror = function(evt) { onError(evt) };

function onOpen(evt) {doSend("Hello");}
function onClose(evt) {log_info ("DISCONNECTED");}
function onMessage(evt) {log_info('');}
function onError(evt) {log_info('ERROR: ' +;}
function doClose () {websocket.close();}
function doSend(message) {
      log_info('SENT: ' + message));
function talk () {
  doSend ($("#mymsg").val());
function log_info (message) {
 $('<li>' + message + '</li>').appendTo ("#display");

If your WSGI app enable handle websocket, it should give initial parameters to Skitai like this,

def websocket (was, message):
  if was.wsinit ():
    return was.wsconfig (
      websocket design specs,
      keep_alive_timeout = 60,
      message_encoding = None

websocket design specs can be choosen one of 4.

WS_SIMPLE (before version 0.24, WEBSOCKET_REQDATA)

  • Thread pool manages n websocket connection
  • It’s simple request and response way like AJAX
  • Use skitai initail thread pool, no additional thread created
  • Low cost on threads resources, but reposne cost is relatvley high than the others

WS_GROUPCHAT (New in version 0.24)

  • Trhead pool manages n websockets connection
  • Chat room model


  • One thread per websocket connection
  • Use when interactives takes long time like websocket version telnet or subprocess stdout streaming
  • New thread created per websocket connection

keep alive timeout is seconds.


Websocket messages will be automatically converted to theses objects. Note that option is only available with Skito-Saddle WSGI container.


General Usages

Handling websocket has 2 parts - event handling and message handling.

Websocket Events

Currently websocket has 3 envets.

  • skitai.WS_EVT_INIT: in handsahking progress
  • skitai.WS_EVT_OPEN: just after websocket configured
  • skitai.WS_EVT_CLOSE: client websocket channel disconnected

When event occured, message is null string, so WS_EVT_CLOSE is not need handle, but WS_EVT_OPEN would be handled - normally just return None value.

At Flask, use like this.

event = request.environ.get ('websocket.event')
if event == skitai.WS_EVT_INIT:
  return request.environ ['websocket.config'] = (...)
if event == skitai.WS_EVT_OPEN:
  return ''
if event == skitai.WS_EVT_CLOSE:
  return ''
if event:
  return '' # should return null string

At Skito-Saddle, handling events is more simpler,

if was.wsinit ():
  return was.wsconfig (spec, timeout, message_type)
if was.wsopened ():
if was.wsclosed ():
if was.wshasevent (): # ignore all events

Handling Message

Message is received by first arg (at below exapmle, message arg), and you response for this by returning value.

@app.route ("/websocket/echo")
def echo (was, message):
  return "ECHO:" + message

Full Example

Websocket method MUST have both of event and message handling parts.

Let’s see full example, client can connect by ws://localhost:5000/websocket/echo.

from skitai.saddle import Saddle
import skitai

app = Saddle (__name__)
app.debug = True
app.use_reloader = True

@app.route ("/websocket/echo")
def echo (was, message):
  #-- event handling
  if was.wsinit ():
    return was.wsconfig (skitai.WS_SIMPLE, 60)
  elif was.wsopened ():
    return "Welcome Client %s" % was.wsclient ()
  elif was.wshasevent ():

  #-- message handling
  return "ECHO:" + message

For getting another args, just add args behind message arg.

num_sent = {}

@app.route ("/websocket/echo")
def echo (was, message, clinent_name):
  global num_sent
  client_id = was.wsclient ()

  if was.wsinit ():
    num_sent [client_id] = 0
    return was.wsconfig (skitai.WS_SIMPLE, 60)
  elif was.wsopened ():
  elif was.wsclosed ():
    del num_sent [client_id]
  elif was.wshasevent ():

  num_sent [client_id] += 1
  return "%s said:" % (clinent_name, message)

Now client can connect by ws://localhost:5000/websocket/chat?client_name=stevemartine.

Once websocket configured by was.wsconfig (), whenever message is arrived from this websocket connection, called this echo method. And you can use all was services as same as other WSGI methods.

was.wsclient () is equivalent to was.env.get (‘websocket.client’) and has numeric unique client id.

For Flask Users

At Flask, Skitai can’t know which variable name receive websocket message, then should specify.

from flask import Flask, request
import skitai

app = Flask (__name__)
app.debug = True
app.use_reloader = True

@app.route ("/websocket/echo")
def echo ():
  event = request.environ.get ('websocket.event')
  client_id = request.environ.get ('websocket.client')

  if event == skitai.WS_EVT_INIT:
    request.environ ["websocket.config"] = (skitai.WS_SIMPLE, 60, ("message",))
    return ""
  elif event == skitai.WS_EVT_OPEN:
    return "Welcome %d" % client_id
  elif event:
    return ""
  return "ECHO:" + request.args.get ("message")

In this case, variable name is (“message”,), It means take websocket’s message as “message” arg.

If returned object is python str type, websocket will send messages as text tpye, if bytes type, as binary. But Flask’s return object is assumed as text type.

Also note, at flask, you should not return None, so you should return null string, if you do not want to send any message.

Send Messages Through Websocket Directly

It needn’t return message, but you can send directly multiple messages through was.websocket,

@app.route ("/websocket/echo")
def echo (was, message):
  if was.wsinit ():
    return was.wsconfig (skitai.WS_SIMPLE, 60)
  elif was.wshasevent (): # ignore all events

  was.websocket.send ("You said," + message)
  was.websocket.send ("I said acknowledge")

This way is very useful for Flask users, because Flask’s return object is bytes, so Skitai try to decode with utf-8 and send message as text type. If Flask users want to send binary data, just send bytes type.

@app.route ("/websocket/echo")
def echo ():
  event = request.environ.get ('websocket.event')
  if event == skitai.WS_EVT_INIT:
    request.environ ["websocket.config"] = (skitai.WS_SIMPLE, 60, ("message",))
    retrurn ''
  elif event:
    return ''

  request.environ ["websocket"].send (
    ("You said, %s" % message).encode ('iso8859-1')

Use Message Encoding

For your convinient, message automatically load and dump object like JSON. But this feature is only available with Skito-Saddle.

@app.route ("/websocket/json")
def json (was, message):
  if was.wsinit ():
    return was.wsconfig (skitai.WS_SIMPLE, 60, skitai.WS_MSG_JSON)
  elif was.wshasevent ():

  return dbsearch (message ['query'], message ['offset'])

JSON message is automatically loaded to Python object, and returning object also will dump to JSON.

Currently you can use WS_MSG_JSON and WS_MSG_XMLRPC. And I guess streaming and multi-chatable gRPC over websocket also possible, I am testing it.

Simple Data Request & Response

Here’s a echo app for showing simple request-respone.

Client can connect by ws://localhost:5000/websocket/chat.

@app.route ("/websocket/echo")
def echo (was, message):
  if was.wsinit ():
    return was.wsconfig (skitai.WS_SIMPLE, 60)
  elif was.wshasevent ():

  return "ECHO:" + message

First args (message) are essential. Although you need other args, you must position after this essential arg.

Group Chat Websocket

This is just extension of Simple Data Request & Response. Here’s simple multi-users chatting app.

This feature will NOT work on multi-processes run mode.

Many clients can connect by ws://localhost:5000/websocket/chat?roomid=1. and can chat between all clients.

@app.route ("/chat")
def chat (was, message, room_id):
  client_id = was.wsclient ()

  if was.wsinit ():
    return was.wsconfig (skitai.WS_GROUPCHAT, 60)
  elif was.wsopened ():
    return "Client %s has entered" % client_id
  elif was.wsclosed ():
    return "Client %s has leaved" % client_id

  return "Client %s Said: %s" % (client_id, message)

In this case, first 2 args (message, room_id) are essential.

For sending message to specific client_id,

clients = list (was.websocket.clients.keys ())
was.websocket.send ('Hi', clients [0])
# OR
return 'Hi', clients [0]

At Flask, should setup for variable names you want to use,

if request.environ.get ("websocket.event") == skitai.WS_EVT_INIT:
  request.environ ["websocket.config"] = (
    ("message", "room_id")
  return ""

Threadsafe-Dedicated Websocket

It is NOT for general customer services. Please read carefully.

This spec is for very special situation. It will create new work thread and that thread handles only one client. And The thread will be continued until message receiving loop is ended. It is designed for long running app and for limited users - firms’s employees or special clients who need to use server-side resources or long applications take long time to finish and need to observe output message stream.

Briefly, it can be helpful for making web version frontend UI to controlling your backend application with jquery, HTML5 easily.

Client can connect by ws://localhost:5000/websocket/talk?name=jamesmilton.

class Calcultor:
  def __init__ (self, ws): = ws
    self.p = None

  def calculate (self, count):
    self.p = Popen (
      [sys.executable, r'', '-c', count],
      stdout=PIPE, shell = False
    for line in iter(p.stdout.readline, ''): (line)
    self.p.stdout.close ()
    self.p = None

  def run (self, count):
    if self.p is None:
      threading.Thread (target = self.calculate, args = (count,)).start ()
      return 1

  def kill (self):
    if self.p:
      os.kill (
      return 1

@app.route ("/websocket/calculate")
def calculate (was):
  if was.wsinit ():
    return was.wsconfig (skitai.WS_DEDICATE, 60)

  ws = was.websocket
  calcultor = Calcultor (ws)
  while 1:
    m = ws.getwait ()
    if m is None: # client disconnected
      calcultor.kill ()

    if m.lower () == "bye":
      calcultor.kill ()
      ws.send ("Bye, have a nice day." + m)
      ws.close ()

    elif m.lower () == "kill":
      if calcultor.kill (): ('killed')
      else: ('Error: not running')

    elif m.lower () [:3] == "run":
      if (int (m [3:].strip ())): ('started')
      else: ('Error: already running')

      ws.send ("You said %s but I can't understatnd" % m)

At Flask,

if request.environ.get ("websocket.event") == skitai.WS_EVT_INIT:
  request.environ ["websocket.config"] = (
  return ""

Request Handling with Skito-Saddle

Saddle is WSGI container integrated with Skitai App Engine.

Flask and other WSGI container have their own way to handle request. So If you choose them, see their documentation.

And note below objects and methods ARE NOT WORKING in any other WSGI containers except Saddle.

Before you begin, recommended Saddle App’s directory structure is like this:

  • Skitai runner
  • File, Main app
  • appack: Directory, Module package for helping app like, etc…
  • static: Directory, Static file like css, js, images. This directory would be mounted for using
  • templates: Directory, Jinaja and Chameleon template files
  • resources: Directory, Various files as app need like sqlite db file. In you app, you use these files, you can access file in resources by app.get_resource (“db”, “sqlite3.db”) like os.path.join manner.

Access Saddle App

You can access all Saddle object from

  • # use for custom configuration like = 1
  • = None
  • = “digest”
  • = False
  • = None
  • = {}
  • () is equal to was.ab ()

Currently has these properties and you can reconfig by setting new value:

  • = 5 * 1024 * 1024
  • = 5 * 1024 * 1024
  • = 20 * 1024 * 1024
  • = 20000000

Debugging and Reloading App

If debug is True, all errors even server errors is shown on both web browser and console window, otherhwise shown only on console.

If use_reloader is True, Skito-Saddle will detect file changes and reload app automatically, otherwise app will never be reloaded.

from skitai.saddle import Saddle

app = Saddle (__name__)
app.debug = True # output exception information
app.use_reloader = True # auto realod on file changed

Reloading App Package

If app.use_reloader is True, all module of appack - sub package directory of - will be reloaded automatically if file is modified.


Basic routing is like this:

@app.route ("/hello")
def hello_world (was):
  return was.render ("hello.htm")

For adding some restrictions:

@app.route ("/hello", methods = ["GET"], content_types = ["text/xml"])
def hello_world (was):
  return was.render ("hello.htm")

If method is not GET, Saddle will response http error code 405 (Method Not Allowed), and content-type is not text/xml, 415 (Unsupported Content Type).


Reqeust object provides these methods and attributes:

  • was.request.method # upper case GET, POST, …
  • was.request.command # lower case get, post, …
  • was.request.uri
  • was.request.version # HTTP Version, 1.0, 1.1
  • was.request.body
  • was.request.headers # case insensitive dictioanry
  • was.request.args # dictionary contains url/form parameters
  • was.request.split_uri () # (script, param, querystring, fragment)
  • was.request.json () # load request body as json
  • was.request.get_header (“content-type”) # case insensitive
  • was.request.get_headers () # retrun header all list
  • was.request.get_body ()
  • was.request.get_scheme () # http or https
  • was.request.get_remote_addr ()
  • was.request.get_user_agent ()
  • was.request.get_content_type ()
  • was.request.get_main_type ()
  • was.request.get_sub_type ()


Basically, just return contents.

@app.route ("/hello")
def hello_world (was):
  return was.render ("hello.htm")

If you need set additional headers or HTTP status,

@app.route ("/hello")
def hello (was):
  return was.response ("200 OK", was.render ("hello.htm"), [("Cache-Control", "max-age=60")])

def hello (was):
  return was.response (body = was.render ("hello.htm"), headers = [("Cache-Control", "max-age=60")])

def hello (was):
  was.response.set_header ("Cache-Control", "max-age=60")
  return was.render ("hello.htm")

Above 3 examples will make exacltly same result.

Sending specific HTTP status code,

def hello (was):
  return was.response ("404 Not Found", was.render ("err404.htm"))

def hello (was):
  # if body is not given, automaticcally generated with default error template.
  return was.response ("404 Not Found")

If app raise exception, traceback information will be displayed only app.debug = True. But you intentionally send it inspite of app.debug = False:

# File
@app.route ("/raise_exception")
def raise_exception (was):
      raise ValueError ("Test Error")
      return was.response ("500 Internal Server Error", exc_info = sys.exc_info ())

You can return various objects.

# File
@app.route ("/streaming")
def streaming (was):
  return was.response ("200 OK", open ("mypicnic.mp4", "rb"), headers = [("Content-Type", "video/mp4")])

# Generator
def build_csv (was):
  def generate():
    for row in iter_all_rows():
      yield ','.join(row) + '\n'
  return was.response ("200 OK", generate (), headers = [("Content-Type", "text/csv")])

All available return types are:

  • String, Bytes, Unicode
  • File-like object has ‘read (buffer_size)’ method, optional ‘close ()’
  • Iterator/Generator object has ‘next() or _next()’ method, optional ‘close ()’ and shoud raise StopIteration if no more data exists.
  • Something object has ‘more()’ method, optional ‘close ()’
  • Classes of skitai.lib.producers
  • List/Tuple contains above objects
  • XMLRPC dumpable object for if you want to response to XMLRPC

The object has ‘close ()’ method, will be called when all data consumed, or socket is disconnected with client by any reasons.

  • was.response (status = “200 OK”, body = None, headers = None, exc_info = None)
  • was.response.set_status (status) # “200 OK”, “404 Not Found”
  • was.response.get_status ()
  • was.response.set_headers (headers) # [(key, value), …]
  • was.response.get_headers ()
  • was.response.set_header (k, v)
  • was.response.get_header (k)
  • was.response.del_header (k)
  • was.response.hint_promise (uri) # New in version 0.16.4, only works with HTTP/2.x and will be ignored HTTP/1.x

Useful Response Shortcuts

When In cases you want to retrun JSON, XMLRPC, gRPC or local file content, below methods will be useful.

@app.route ("/")
def getjson (was):
  return was.jstream ({'mydata': 'myvalue'})

@app.route ("/<filename>")
def getfile (was, filename):
  return was.fstream ('/data/%s' % filename)
  • was.jstream (obj) # shortcut for was.response (“200 OK”, was.tojson (obj), [(“Content-Type”, “application/json”)])
  • was.xstream (obj, usedatetime = 0) # shortcut for was.response (“200 OK”, was.toxml (obj), [(“Content-Type”, “text/xml”)])
  • was.gstream (obj) # shortcut for was.response (“200 OK”, was.togrpc (obj), [(“Content-Type”, “application/grpc”)])
  • was.fstream (abspath, mimetype = ‘application/octet-stream’) # return file stream object

Async Streaming Response

New in version 0.24.8

If you use was’ requests services, and they’re expected taking a long time to fetch, you can use async response.

  • Async response has advantage at multi threads environment returning current thread to thread pool early for handling the other requests
  • Async response should be used at single thread evironment. If you run Skitai with threads = 0, you can’t use wait(), getwait() or getswiat() for receiving response for HTTP/DBO requests.
def response_handler (resp, proxy):
  if resp.status_code == 200:
    proxy [resp.reqid]  = proxy.render (
      '%s.html' % resp.reqid,
      r = response
    proxy [resp.reqid] = '<div>Error in %s</div>' % resp.reqid

  if proxy.fetched_all ():
    proxy.done (proxy.render_all ("example.html"))
    # or just join response data
    # proxy.done (proxy ['skitai'] + "<hr>" + proxy ['aquests'])

@app.route ("/aresponse_example")
def aresponse_example (was):
  proxy = was.aresponse (response_handler)
  proxy.get ('skitai', "")
  proxy.get ('aquests', "")
  return proxy

‘skitai.html’ Jinja2 template used in render() is,

<div>{{ r.url }} </div>
<div>{{ r.text }}</div>

‘example.html’ Jinja2 template used in render_all() is,

<div>{{ skitai }}</div>
<div>{{ aquests }}</div>

And you can use almost was.* objects at render() and render_all() like was.request,, was.ab or was.g etc. But remember that response header had been already sent so you cannot use aquests features and connot set new header values like cookie or mbox (but reading is still possible).

Above proxy can make requests as same as was object except first argument is identical request name (reqid). Compare below things.

This identifier can handle responses at executing callback. reqid SHOULD follow Python variable naming rules because might be used as template variable.

You MUST call ResProxy.done(content_to_send) finally, and if you have chunk content to send, you can call ResProxy.push(chunk_content_to_send) for sending middle part of contents before calling done ().

New in version 0.25.2

You can set meta data dictionary per requests if you need.

def response_handler (response, proxy):
  due = time.time () - response.meta ['created']
  proxy.push (response.content)
  proxy.push ('\n\nFetch in %2.3f seconds' % due)
  proxy.done () # Should call

@app.route ("/aresponse_example")
def aresponse_example (was):
  proxy = was.aresponse (response_handler)
  proxy.get ('req-0', "", meta = {'created': time.time ()})
  return was.response ("200 OK", proxy, [('Content-Type', 'text/plain')])

But it is important that meta arg should be as keyword arg, and DON’T use ‘__reqid’ as meta data key. ‘__reqid’ is used internally.

Creating async response proxy:

  • was.aresponse (response_handler, prolog = None, epilog = None): return ResProxy, prolog and epilog is like html header and footer

response_handler should receive 2 args: response for your external resource request and ResProxy.

Note: It’s impossible requesting map-reduce requests at async response mode.

collect_producer has these methods.

  • ResProxy.get (), post (), …
  • ResProxy.fetched_all (): True if numer of requests is same as responses
  • ResProxy.render (template_file, single dictionary object or keyword args, …): render per response, and can assign into ResProxy like dictionary
  • ResProxy.render_all (template_file): render all responses, in template file, reqids of each responses are used as template variable.
  • ResProxy.push (content_to_send): push chunk data to channel
  • ResProxy.done (content_to_send = None)

HTTP/2.0 Server Push

New in version 0.16

Skiai supports HTPT2 both ‘h2’ protocl over encrypted TLS and ‘h2c’ for clear text (But now Sep 2016, there is no browser supporting h2c protocol).

Basically you have nothing to do for HTTP2. Client’s browser will handle it except HTTP2 server push.

For using it, you just call was.response.hint_promise (uri) before return response data. It will work only client browser support HTTP2, otherwise will be ignored.

@app.route ("/promise")
def promise (was):

  was.response.hint_promise ('/images/A.png')
  was.response.hint_promise ('/images/B.png')

  return was.response (
    "200 OK",
      'Promise Sent<br><br>'
      '<img src="/images/A.png">'
      '<img src="/images/B.png">'

Getting URL Parameters

@app.route ("/hello")
def hello_world (was, num = 8):
  return num

@app.route ("/hello/<int:num>")
def hello_world (was, num = 8):
  return str (num)

Also you can access as dictionary object ‘was.request.args’.

num = was.request.args.get ("num", 0)

for fancy url building, available param types are:

  • int
  • float
  • path: /download/<int:major_ver>/<path>, should be positioned at last like /download/1/version/1.1/win32
  • If not provided, assume as string. and all space char replaced to “_’

Getting Form Parameters

Getting form is not different from the way for url parameters, but generally form parameters is too many to use with each function parameters, can take from single args **form or take mixed with named args and **form both.

@app.route ("/hello")
def hello (was, **form):
      return "Post %s %s" % (form.get ("userid", ""), form.get ("comment", ""))

@app.route ("/hello")
def hello_world (was, userid, **form):
      return "Post %s %s" % (userid, form.get ("comment", ""))

Building URL

If your app is mounted at “/math”,

@app.route ("/add")
def add (was, num1, num2):
  return int (num1) + int (num2) ("add", 10, 40) # returned '/math/add?num1=10&num2=40'

# BUT it's too long to use practically,
# was.ab is acronym for
was.ab ("add", 10, 40) # returned '/math/add?num1=10&num2=40'
was.ab ("add", 10, num2=60) # returned '/math/add?num1=10&num2=60'

@app.route ("/hello/<name>")
def hello (was, name = "Hans Roh"):
  return "Hello, %s" % name

was.ab ("hello", "Your Name") # returned '/math/hello/Your_Name'

Access Environment Variables

was.env is just Python dictionary object.

if "HTTP_USER_AGENT" in was.env:
was.env.get ("CONTENT_TYPE")

Jinja2 Template Engine

Although You can use any template engine, Skitai provides was.render() which uses Jinja2 template engine. For providing arguments to Jinja2, use dictionary or keyword arguments.

return was.render ("index.html", choice = 2, product = "Apples")

#is same with:

return was.render ("index.html", {"choice": 2, "product": "Apples"})


return was.render ("index.html", {"choice": 2}, product = "Apples")

Directory structure sould be:

  • /project_home/
  • /project_home/templates/index.html

At template, you can use all ‘was’ objects anywhere defautly. Especially, Url/Form parameters also can be accessed via ‘was.request.args’.

{{ was.cookie.username }} choices item {{ was.request.args.get ("choice", "N/A") }}.

<a href="{{ was.ab ('checkout', choice) }}">Proceed</a>

Also ‘was.g’ is can be useful in case threr’re lots of render parameters.

was.g.product = "Apple"
was.g.howmany = 10

return was.render ("index.html")

And at jinja2 template,

{% set g = was.g }} {# make shortcut #}
Checkout for {{ g.howmany }} {{ g.product }}{{g.howmany > 1 and "s" or ""}}

If you want modify Jinja2 envrionment, can through object.

def generate_form_token ():
  ...['form_token'] = generate_form_token

New in version 0.15.16

Added new app.jinja_overlay () for easy calling app.jinja_env.overlay ().

Recently JS HTML renderers like Vue.js, React.js have confilicts with default jinja mustache variable. In this case you mightbe need change it.

app = Saddle (__name__)
app.debug = True
app.use_reloader = True
app.jinja_overlay (
  variable_start_string = "{{",
  variable_end_string = "}}",
  block_start_string = "{%",
  block_end_string = "%}",
  comment_start_string = "{#",
  comment_end_string = "#}",
  line_statement_prefix = "%",
  line_comment_prefix = "%%"

if you set same start and end string, please note for escaping charcter, use double escape. for example ‘#’, use ‘##’ for escaping.

Warning: Current Jinja2 2.8 dose not support double escaping (##) but it will be applied to runtime patch by Saddle. So if you use app.jinja_overlay, you have compatible problems with official Jinja2.

Chameleon Template Engine

New in version 0.26.6

For using Chameleon template engine, you just make template file extention with ‘.pt’ or ‘.ptal’ (Page Template or Page Template Attribute Language).

return was.render ("index.ptal", choice = 2, product = "Apples")

Access Session

was.session has almost dictionary methods.

To enable session for app, random string formatted securekey should be set for encrypt/decrypt session values.

WARN: securekey should be same on all skitai apps at least within a virtual hosing group, Otherwise it will be serious disaster.

app.securekey = "ds8fdsflksdjf9879dsf;?<>Asda"
app.session_timeout = 1200 # sec

@app.route ("/session")
def hello_world (was, **form):
  if "login" not in was.session:
    was.session.set ("user_id", form.get ("hansroh"))
    # or
    was.session ["user_id"] = form.get ("hansroh")
  • was.session.set (key, val)
  • was.session.get (key, default = None)
  • was.session.source_verified (): If current IP address matches with last IP accesss session
  • was.session.getv (key, default = None): If not source_verified (), return default
  • was.session.remove (key)
  • was.session.clear ()
  • was.session.kyes ()
  • was.session.values ()
  • was.session.items ()
  • was.session.has_key ()
  • was.session.iterkyes ()
  • was.session.itervalues ()
  • was.session.iteritems ()

Messaging Box

Like Flask’s flash feature, Skitai also provide messaging tool.

@app.route ("/msg")
def msg (was):
  was.mbox.send ("This is Flash Message", "flash")
  was.mbox.send ("This is Alert Message Kept by 60 seconds on every request", "alram", valid = 60)
  return was.redirect (was.ab ("showmsg", "Hans Roh"), status = "302 Object Moved")

@app.route ("/showmsg")
def showmsg (was, name):
  return was.render ("msg.htm", name=name)

A part of msg.htm is like this:

Messages To {{ name }},
      {% for message_id, category, created, valid, msg, extra in was.mbox.get () %}
              <li> {{ mtype }}: {{ msg }}</li>
      {% endfor %}

Default value of valid argument is 0, which means if page called was.mbox.get() is finished successfully, it is automatically deleted from mbox.

But like flash message, if messages are delayed by next request, these messages are save into secured cookie value, so delayed/long term valid messages size is limited by cookie specificatio. Then shorter and fewer messsages would be better as possible.

‘was.mbox’ can be used for general page creation like handling notice, alram or error messages consistently. In this case, these messages (valid=0) is consumed by current request, there’s no particular size limitation.

Also note valid argument is 0, it will be shown at next request just one time, but inspite of next request is after hundred years, it will be shown if browser has cookie values.

def before_request (was):
  if has_new_item ():
    was.mbox.send ("New Item Arrived", "notice")

@app.route ("/main")
def main (was):
  return was.render ("news.htm")

news.htm like this:

News for {{ was.g.username }},
      {% for mid, category, created, valid, msg, extra in was.mbox.get ("notice", "news") %}
              <li class="{{category}}"> {{ msg }}</li>
      {% endfor %}
  • was.mbox.send (msg, category, valid_seconds, key=val, …)
  • was.mbox.get () return [(message_id, category, created_time, valid_seconds, msg, extra_dict)]
  • was.mbox.get (category) filtered by category
  • was.mbox.get (key, val) filtered by extra_dict
  • was.mbox.source_verified (): If current IP address matches with last IP accesss mbox
  • was.mbox.getv (…) return get () if source_verified ()
  • (key, val): find in extra_dict. if val is not given or given None, compare with category name. return [message_id, …]
  • was.mbox.remove (message_id)

Named Session & Messaging Box

New in version 0.15.30

You can create multiple named session and mbox objects by mount() methods.

was.session.mount (
  name = None, securekey = None,
  path = None, domain = None, secure = False, http_only = False,
  session_timeout = None

was.mbox.mount (
  name = None, securekey = None,
  path = None, domain = None, secure = False, http_only = False

For example, your app need isolated session or mbox seperated default session for any reasons, can create session named ‘ADM’ and if this session or mbox is valid at only /admin URL.

def index (was):
  was.session.mount ("ADM", SECUREKEY_STRING, path = '/admin')
  was.session.set ("admin_login", True)

  was.mbox.mount ("ADM", SECUREKEY_STRING, path = '/admin')
  was.mbox.send ("10 data has been deleted", 'warning')

SECUREKEY_STRING needn’t same with app.securekey. And path, domain, secure, http_only args is for session cookie, you can mount any named sessions or mboxes with upper cookie path and upper cookie domain. In other words, to share session or mbox with another apps, path should be closer to root (/).

def index (was):
  was.session.mount ("ADM", SECUREKEY_STRING, path = '/')
  was.session.set ("admin_login", True)

Above ‘ADM’ sesion can be accessed by all mounted apps because path is ‘/’.

Also note was.session.mount (None, SECUREKEY_STRING) is exactly same as mounting default session, but in this case SECUREKEY_STRING should be same as app.securekey.

mount() is create named session or mbox if not exists, exists() is just check wheather exists named session already.

if not was.session.exists (None):
  return "Your session maybe expired or signed out, please sign in again"

if not was.session.exists ("ADM"):
  return "Your admin session maybe expired or signed out, please sign in again"

File Upload

FORM = """
  <form enctype="multipart/form-data" method="post">
  <input type="hidden" name="submit-hidden" value="Genious">
  <p></p>What is your name? <input type="text" name="submit-name" value="Hans Roh"></p>
  <p></p>What files are you sending? <br />
  <input type="file" name="file">
  <input type="submit" value="Send">
  <input type="reset">

@app.route ("/upload")
def upload (was, *form):
  if was.request.command == "get":
    return FORM
    file = form.get ("file")
    if file: ("d:\\var\\upload", dup = "o") # overwrite

‘file’ object’s attributes are:

  • file.path: temporary saved file full path
  • original file name posted
  • file.size
  • file.mimetype
  • file.remove ()
  • (into, name = None, mkdir = False, dup = “u”)
    • if name is None, used
    • dup:
      • u - make unique (default)
      • o - overwrite

App and Method Decorators and was.g

Method decorators called automatically when each method is requested in a app.

def before_request (was):
  if not login ():
    return "Not Authorized"

def finish_request (was):

def failed_request (was, exc_info):

def teardown_request (was):
  was.g.resouce.close ()

@app.route ("/view-account")
def view_account (was, userid):
  was.g.user_id = "jerry"
  was.g.user_status = "active"
  was.g.resouce = open ()
  return ...

For this situation, ‘was’ provide was.g that is empty class instance. was.g is valid only in current request. After end of current request.

If view_account is called, Saddle execute these sequence:

    content = before_request (was)
    if content:
      return content
    content = view_account (was, *args, **karg)

    content = failed_request (was, sys.exc_info ())
    if content is None:

    finish_request (was)

  teardown_request (was)

return content

Be attention, failed_request’s 2nd arguments is sys.exc_info (). Also finish_request and teardown_request (NOT failed_request) should return None (or return nothing).

If you handle exception with failed_request (), return custom error content, or exception will be reraised and Saddle will handle exception.

New in version 0.14.13

 def failed_request (was, exc_info):
   # releasing resources
   return was.response (
       "501 Server Error",
       was.render ("err501.htm", msg = "We're sorry but something's going wrong")

Also there’re another kind of decorator group, App decorators.

def startup (wasc):
  logger = wasc.logger.get ("app")
  # OR
  logger = wasc.logger.make_logger ("login", "daily")
  config = wasc.config
  wasc.register ("loginengine", SNSLoginEngine (logger))
  wasc.register ("searcher", FulltextSearcher (wasc.numthreads))

def onreload (wasc):
  wasc.loginengine.reset ()

def shutdown (wasc):
  wasc.searcher.close ()

  wasc.unregister ("loginengine")
  wasc.unregister ("searcher")

‘wasc’ is Python Class object of ‘was’, so mainly used for sharing Skitai server-wide object via was.object.

And you can access numthreads, logger, config from wasc.

As a result, myobject can be accessed by all your current app functions even all other apps mounted on Skitai.

# app mounted to ''
@app.route ("/")
def index (was):
  was.loginengine.get_user_info ()
  was.searcher.query ("ipad")

# app mounted to ''
@app.route ("/")
def index (was):
  was.loginengine.check_user_to ("facebook")
  was.searcher.query ("ipad")

# app mounted to ''
@app.route ("/")
def index (was):
  was.searcher.query ("news")

Note: The way to mount with host, see ‘Mounting With Virtual Host’ chapter below.

It maybe used like plugin system. If a app which should be mounted loads pulgin-like objects, theses can be used by Skitai server wide apps via was.object1, was.object2,…

These methods will be called,

  1. startup: when app imported on skitai server started
  2. onreload: when app.use_reloader is True and app is reloaded
  3. shutdown: when skitai server is shutdowned

Building Cache With App Decorator

New in version 0.26

If you have pre-defined database cluster, and want to create cache object on app starting, you can use was.ajob method.

app.cache = {}

def create_cache (res):
  for row in
    app.cache ['STATENAMES'][row.code] =

def startup (wasc):
  wasc.ajob ('@mydb', create_cache).execute ("select code, name from states;")

Now you can access cache by or app.cache.


Changed in version 0.15.21

  • removed app.user and app.password
  • add app.users object has get(username) methods like dictionary

Saddle provide simple authenticate for administration or perform access control from other system’s call.

Authentication On Entire App

app = Saddle (__name__)

app.authorization = "digest"
app.realm = "Partner App Area of"
app.users = {"app": ("iamyourpartnerapp", 0, {'role': 'root'})}
app.authenticate = True

@app.route ("/hello/<name>")
def hello (was, name = "Hans Roh"):
  return "Hello, %s" % name

If app.authenticate is True, all routes of app require authorization (default is False).

Authentication On Specific Methods Only

Otherwise you can make some routes requirigng authorization like this:

# False is default, you can omit this line
app.authenticate = False

@app.route ("/hello/<name>", authenticate = True)
def hello (was, name = "Hans Roh"):
  return "Hello, %s" % name

User Collection

The return of app.users.get (username) can be:

  • (str password, boolean encrypted, obj userinfo)
  • (str password, boolean encrypted)
  • str password

If you use encrypted password, you should use digest authorization and password should encrypt by this way:

from hashlib import md5

encrypted_password = md5 (
      ("%s:%s:%s" % (username, realm, password)).encode ("utf8")
).hexdigest ()

If authorization is successful, app can access username and userinfo vi was.request.user.

  • was.request.user.realm

If your server run with SSL, you can use app.authorization = “basic”, otherwise recommend using “digest” for your password safety.

Building for Larger App

You have 2 options for extending your app scale.

  1. Mount multiple microservices
  2. Mount saddlery on saldde

Mount Multiple Microservices

I personally recommend this way by current developing trend.

import skitai (
  mount = [
    ('/service', ('/service/app', 'app')),
    ('/service/trade', ('/service/trade/app', 'app')),
    ('/service/intro', ('/service/intro/app', 'app')),
    ('/service/admin', ('/service/admin/app', 'app')),
    ('/', '/service/static')

And your pysical directory structure is,






This structure make highly focus on each microservices and make easy to move or apply scaling by serivce traffic increment.

Mount Saddlery On Saddle

If your app is very large or want to manage codes by categories, you can seperate your app.

from skitai.saddle import Saddlery
part = Saddlery ()

@part.route ("/<name>")
def hello (was):
  # can build other module's method url
  return was.ab ("index", 1, 2)

from skitai.saddle import Saddle
from . import admin

app = Saddle (__name__)
app.debug = True
app.use_reloader = True
app.mount ("/admin", admin, "part")

@app.route ("/")
def index (was, num1, num2):
  return was.ab ("hello", "Hans Roh") # url building

Now, hello function’s can be accessed by ‘/[app mount point]/admin/Hans_Roh’.

App’s configs like debug & use_reloader, etc, will be applied to packages except decorators.

Note: is always main Saddle app NOT current Saddlery sub app.

Saddlery can have own sub saddlery and decorators.

from skitai.saddle import Saddlery
from . import admin_sub

part = Saddlery () # mount point
# Saddlery also can have sub Saddlery
part.mount ("/admin/sub", admin_sub, "app")

def startup (wasc):
  wasc.register ("loginengine", SNSLoginEngine ())
  wasc.register ("searcher", FulltextSearcher ())

def shutdown (wasc):
  wasc.searcher.close ()

  wasc.unregister ("loginengine")
  wasc.unregister ("searcher")

def before_request (was):
  if not login ():
    return "Not Authorized"

def teardown_request (was):
  was.g.resouce.close ()

@part.route ("/<name>")
def hello (was):
  # can build other module's method url
  return was.ab ("index", 1, 2)

In this case, app and sub-app’s method decorators are nested executed in this order.

  sub-app.finish_request() or package.failed_request()
  sub-app.teardown_request ()
app.finish_request() or app.failed_request()
app.teardown_request ()

Saddlery and Jinja2 Templates

was.render (template_path) always find templates directory where exists, even if is located in sub directory with package form. This is somewhat conflicated but I think it’s more easier way to maintain template files and template include policy. Remeber one app can have one templates directoty. But you can seperate into templates files by sub directory. For example:


But if you want to use independent templates under own templates directory:

from skitai.saddle import Saddlery

part = Saddlery (__name__)

@part.route ("/<name>")
def hello (was):
  return was.render2 ("show.htm", name = name)

Implementing XMLRPC Service

Client Side:

import aquests

stub = aquests.rpc ("")
stub.add (10000, 5000)
fetchall ()

Server Side:

@app.route ("/add")
def index (was, num1, num2):
  return num1 + num2

Is there nothing to diffrence? Yes. Saddle app methods are also used for XMLRPC service if return values are XMLRPC dumpable.

Implementing gRPC Service

Client Side:

import aquests
import route_guide_pb2

stub = aquests.grpc ("")
point = route_guide_pb2.Point (latitude=409146138, longitude=-746188906)
stub.GetFeature (point)
aquests.fetchall ()

Server Side:

import route_guide_pb2

@app.route ("/GetFeature")
def GetFeature (was, point):
  feature = get_feature(db, point)
if feature is None:
  return route_guide_pb2.Feature(name="", location=point)
  return feature

if __name__ == "__main__": (
    mount = [('/routeguide.RouteGuide', app)

For more about gRPC and route_guide_pb2, go to gRPC Basics - Python.

Note: I think I don’t understand about gRPC’s stream request and response. Does it means chatting style? Why does data stream has interval like GPS data be handled as stream type? If it is chat style stream, is it more efficient that use proto buffer on Websocket protocol? In this case, it is even possible collaborating between multiple gRPC clients.

Logging and Traceback

If Skitai run with -v option, app and exceptions are displayed at your console, else logged at files.

@app.route ("/")
def sum ():
  was.log ("called index", "info")
      was.log ("exception occured", "error")
      was.traceback ()
  was.log ("done index", "info")

Note inspite of you do not handle exception, all app exceptions will be logged automatically by Saddle. And it includes app importing and reloading exceptions.

  • was.log (msg, category = “info”)
  • was.traceback (id = “”) # id is used as fast searching log line for debug, if not given, id will be Global transaction ID/Local transaction ID

Project Purpose

Skitai App Engine’s original purpose is to serve python fulltext search engine Wissen which is my another pypi work. And I found that it is possibly useful for building and serving websites.

Anyway, I am modifying my codes to optimizing for enabling service on Linux machine with relatvely poor H/W (ex. AWS t2.nano instance) and making easy to auto-scaling provided cloud computing service like AWS.

If you need lots of outside http(s) resources connecting jobs and use PostgreSQL, it might be worth testing and participating this project.

Also note it might be more efficient that circumstance using Gevent WSGI Server + Flask. They have well documentation and already tested by lots of users.

Change Log

0.26 (Apr 2017)

  • 0.26
    • runtime app preferences
    • fix route caching
    • auto reload sub modules in appack directory, if app.use_reloader = True
    • new was.request.json ()
    • integrated with skitaid package, single app file can contain all configure options
    • level down developement status to alpha
    • fix sqlite3 closing

0.25 (Feb 2017)

  • 0.25.7: fix fancy url, non content-type header post/put request
  • 0.25.6: add Chameleon template engine
  • 0.25.5: app.jinja_overlay ()’s default args become jinja2 default
  • fix proxy retrying
  • 0.25.4 license changed from BSD to MIT, fix websocket init at single thread
  • 0.25.3 aresponse response handler args spec changed, class name is cahnged from AsyncResponse to ResProxy
  • 0.25.2 fix aresponse exception handling, aresponse can send streaming chunk data
  • 0.25.1 change app.jinja_overlay () default values and number of args, remove raw line statement
  • project name chnaged: Skitai Library => Skitai App Engine

0.24 (Jan 2017)

  • 0.24.9 bearer token handler spec changed
  • 0.24.8 add async response, fix await_fifo bug
  • 0.24.7 fix websocket shutdown
  • 0.24.5 eliminate client arg from websocket config
  • 0.24.5 eliminate event arg from websocket config
  • fix proxy tunnel
  • fix websocket cleanup
  • change websocket initializing, not lower version compatible
  • WEBSOCKET_MULTICAST deprecated, and new WEBSOCKET_GROUPCHAT does not create new thread any more

0.23 (Jan 2017)

  • ready_producer_fifo only activated when proxy or reverse proxy is enabled, default deque will be used
  • encoding argument was eliminated from REST call
  • changed RPC, DBO request spec
  • added gRPC as server and client
  • support static files with http2
  • fix POST method on reverse proxying

0.22 (Jan 2017)

  • 0.22.7 fix was.upload(),*()
  • 0.22.5 fix xml-rpc service
  • 0.22.4 fix proxy
  • 0.22.3
    • fix https REST, XML-RPC call
    • fix DB pool
  • 0.22
    • Skitai REST/RPC call now uses HTTP2 if possible
    • Fix HTTP2 opening with POST method
    • Add logging on disconnecting of Websocket, HTTP2, Proxy Tunnel channels
    • See News

0.21 (Dec 2016)

  • 0.21.17 - fix JWT base64 padding problem
  • 0.21.8 - connected with MongoDB asynchronously
  • 0.21.3 - add JWT (JSON Web Token) handler, see Skitai WSGI App Engine Daemon
  • 0.21.2 - applied global/local-transaction-ID to app logging: was.log (msg, logtype), was.traceback ()
  • 0.21 - change request log format, add global/local-transaction-ID to log file for backtrace

0.20 (Dec 2016)

  • 0.20.15 - minor optimize asynconnect, I wish
  • 0.20.14 - fix Redis connector’s threading related error
  • 0.20.4 - add Redis connector
  • 0.20 - add API Gateway access handler

0.19 (Dec 2016)

  • Reengineering was.request methods, fix disk caching

0.18 (Dec 2016)

  • 0.18.11 - default content-type of, was.put() has been changed from ‘application/x-www-form-urlencoded’ to ‘application/json’. if you use this method currently, you SHOULD change method name to was.postform()
  • 0.18.7 - response contents caching has been applied to all was.request services (except websocket requests).

0.17 (Oct 2016)

0.16 (Sep 2016)

  • 0.16.20 fix SSL proxy and divide into package for proxy & websocket_handler
  • 0.16.19 fix HTTP2 cookie
  • 0.16.18 fix handle large request body
  • 0.16.13 fix thread locking for h2.Connection
  • 0.16.11 fix pushing promise and response on Firefox
  • 0.16.8 fix pushing promise and response
  • 0.16.6 add several configs to for limiting post body size from client
  • 0.16.5 add method: was.response.hint_promise (uri) for sending HTP/2 PUSH PROMISE frame
  • 0.16.3 fix flow control window
  • 0.16.2 fix HTTP/2 Uprading for “http” URIs (RFC 7540 Section 3.2)
  • 0.16 HTTP/2.0 implemented with hyper-h2

0.15 (Mar 2016)

  • fixed fancy URL <path> routing
  • add Websocket design spec: WEBSOCKET_DEDICATE_THREADSAFE
  • fixed Websocket keep-alive timeout
  • fixed fancy URL routing
  • ‘was.cookie.set()’ method prototype has been changed.
  • added Named Session & Messaging Box
  • fix select error when closed socket, thanks to spam-proxy-bots
  • add mimetypes for .css .js
  • fix debug output
  • fix asynconnect.maintern
  • fix loosing end of compressed content
  • fix app reloading, @shutdown
  • fix XMLRPC response and POST length
  • add (), change spec was.mbox.get ()
  • fix routing bugs & was.ab()
  • add saddle.Saddlery class for app packaging
  • @app.startup, @app.onreload, @app.shutdown arguments has been changed

0.14 (Feb 2016)

  • fix proxy occupies CPU on POST method failing
  • was.log(), was.traceback() added
  • fix valid time in message box
  • changed @failed_request arguments and can return custom error page
  • changed command line options, see ‘ –help’
  • batch task scheduler added
  • e-mail sending fixed
  • was.session.getv () added
  • was.response spec. changed
  • SQLite3 DB connection added

0.13 (Feb 2016)

  • was.mbox, was.g, was.redirect, was.render added
  • SQLite3 DB connection added

0.12 (Jan 2016) - Re-engineering ‘was’ networking, PostgreSQL & proxy modules

0.11 (Jan 2016) - Websocket implemeted

0.10 (Dec 2015) - WSGI support

File Type Py Version Uploaded on Size
skitai-0.26b31.tar.gz (md5) Source 2017-04-21 124KB