Skip to main content

A really simple WSGI way to serve static (or mixed) content.

Project description

https://travis-ci.org/rmohr/static3.svg?branch=master

This software is a Python3 compatible fork of Luke Arnos library static.

The library is now Python3 compatible and Genshi support (the sucessor of kid) is added. On Python2 Genshi and/or kid can be used as template engine. On Python3 only Genshi is available.

This library provides an easy way to include static content in your WSGI applications. There is a convenience method for serving files located via pkg_resources. There are also facilities for serving mixed (static and dynamic) content using “magic” file handlers. Python builtin string substitution, kid and Genshi template support are provided and it is easy to roll your own handlers. Note that this distribution does not require kid or Genshi unless you want to use that type of template. Also provides a command of the same name as a convenience when you just want to share a little content over HTTP, ad hoc.

Installation and Usage

Latest release via PIP:

pip install static3

Installation via GitHub:

git clone https://github.com/rmohr/static3.git
cd static3
pip install .

Cling

Cling serves static content only. Just give it the base directory with your files you want to make accessible. You get a full WSGI app with an example as simple as that:

from static import Cling
from wsgiref.simple_server import make_server
my_app = Cling("/my/directory")
make_server("localhost", 9999, my_app).serve_forever()

Now you can access everything in the given directory via http://localhost:9999.D

Serving compressed files

If a gzip compressed file with the ´gz´ postfix is present, it is served, along with the corresponding headers. So if the file ‘index.html’ and the file ‘index.html.gz’ are present, the file ‘index.html.gz’ is served, if the the client indicated that it supports gzipped content.

Shock

Shock has the same basic functionality like Cling but with Shock we can also have some templating fun. Shock comes with three templating backends. String substitution, kid and Genshi. The decision which backend to use is done by examining the extension of the file to serve. The file extensions are ‘stp’, ‘kid’ and ‘genshi’. So if you want to provide a file called ‘index.html’ via the kid backend, name your file ‘index.html.kid’. A short example might look like this:

from static import Shock, KidMagic
from wsgiref.simple_server import make_server
my_app = Shock("/my/directory", magics=[KidMagic(title="Hello World")])
make_server("localhost", 9999, my_app).serve_forever()

And the example ‘index.html.kid’:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:py="http://purl.org/kid/ns#"
  xml:lang="en">
  <head>
  </head>
  <body>
    <h1>$title</h1>
  </body>
</html>

A similar template ‘index.html.genshi’ for Genshi:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:py="http://genshi.edgewall.org/"
  xml:lang="en">
  <head>
  </head>
  <body>
    <h1>$title</h1>
  </body>
</html>

This simple application will replace the placeholder title with ‘Hello World’ in every provided file which ends in ‘.kid’. In this example it already is already obvious, that although different template engines can be used, they can only be used in a very static way. Never the less Shock is perfectly suitable for simple semi-static things like make the URL to your companies logo, or the company name variable.

Unicode Support

When using a template system in Python3 it might be necessary to explicitly set an encoding for the sites provided. This can be done via the encoding attribute of Shock:

from static import Shock
shock = Shock("/var/www/pub")
shock.encoding="latin-1"

When using Cling or Shock to serve static content the encoding need not to be set, as the content is just streamed through. If you have templates encoded in different formats, an instance of Shock needs to be instantiated for every codec used.

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

static3-0.6.1.tar.gz (20.5 kB view details)

Uploaded Source

File details

Details for the file static3-0.6.1.tar.gz.

File metadata

  • Download URL: static3-0.6.1.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for static3-0.6.1.tar.gz
Algorithm Hash digest
SHA256 2f63f2972bf80e873607bc51a69c9238c9da89491e27f7e5622530210f744ee1
MD5 b6a6c1c8af01c5682a8acae1a28334da
BLAKE2b-256 4b0b23ad258c78c61ddd032c98dd0d838ade2f82a4aa8fcf6e33f01619325ad4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page