slowdown.mapfs – Mapping URLs to filesystem locations

This module provides a tiny web framework that simply maps the URL to the disk location.

Example:

import gevent
import gevent.server
import slowdown.fs
import slowdown.http
import slowdown.mapfs

fs = slowdown.fs.FS()  # locl filesystem
mapfs = \
    slowdown.mapfs.Mapfs(

        # Mapfs requires an FS object to indicate a specific
        # filesystem that contains static files and scripts.
        fs = fs,

        www='/PATH/TO/DOCUMENT/ROOT',  # static files directory
        cgi='/PATH/TO/SCRIPTS'         # scripts directory
    )

server = \
    gevent.server.Server(
        ('0.0.0.0', 8080),
        slowdown.http.Handler(mapfs)
    )
server.start()           # start the server
io_jobs = fs.spawn()     # begin IO loops
gevent.joinall(io_jobs)

Static files in the folder specified by the www parameter shall be sent to the browser. And scripts in the scripts folder will be executed when requested.

The static file will be sent if the file and script are matched by the same URL. If no files or scripts are matched, the existing index.html file or index.html.py script will be the choice.

Swap files and hidden files whose names start with dot, end with tilde ~ , and have .swp, .swx suffixes are ignored.

Script samples:

# file: pkgs/__cgi__/a/b/c/test1.py
# test: http(s)://ROUTER/a/b/c/test1/d/e/f

import slowdown.cgi

def GET(rw):  # only GET requests are processed
    path1       = rw.environ['PATH_INFO']  # -> the original path
    path2       = rw.environ['locals.path_info']    # -> /d/e/f/
    script_name = rw.environ['locals.script_name']  # -> /a/b/c/test1
    return \
        rw.send_html_and_close(
            content='<html>It works!</html>'
        )

def POST(rw):  # only POST requests are processed
    form = slowdown.cgi.Form(rw)
    return \
        rw.send_html_and_close(
            content=f'<html>{form}</html>'
        )
# file: pkgs/__cgi__/a/b/c/d/test2.py
# test: http(s)://ROUTER/a/b/c/d/test2/e/f/g

import slowdown.cgi

# You can define a handler called HTTP to handle all
# request methods just in one place.
#
# Be ware, don't define HTTP and GET/POST at the same time.
def HTTP(rw):
    path_info   = rw.environ['locals.path_info'  ]  # -> /e/f/g
    script_name = rw.environ['locals.script_name']  # -> /a/b/c/d/test2
    if 'GET' == rw.environ['REQUEST_METHOD']:
        return \
            rw.send_html_and_close(
                content='<html>It works!</html>'
            )
    elif 'POST' == rw.environ['REQUEST_METHOD']:
        form = slowdown.cgi.Form(rw)
        return \
            rw.send_html_and_close(
                content=f'<html>{form}</html>'
            )
    else:
        return rw.method_not_allowed()
class slowdown.mapfs.Mapfs(fs: slowdown.fs.FS, www: str, cgi: str, max_file_cache_size: int = 2097152, index_html: str = 'index.html', index_script: str = 'index.html')

A tiny web framework that simply maps the URL to the disk location.

Parameters
  • fs (slowdown.fs.FS) – the filesystem that contains static files and scripts.

  • www (str) – the directory where static files are stored.

  • cgi (str) – the directory where scripts are stored.

__call__(rw: slowdown.http.File)