S
smisk

core

This module is the foundation of Smisk and is implemented in machine native code. See C API for documentation of the C interface.

Requireslibfcgi

Attributes

smisk.core.__build__

Build identifier in URN form, distinguishing each unique build. See smisk.release.build.

Changed in version 1.1.0: Prior to 1.1.0, this was an arbitrary per-build unique string. In 1.1.0 it is now a URN.
smisk.core.app

Current Application (None if no application has been created). This is actually a smisk.util.objectproxy.ObjectProxy. Application.current is the most performance-effective way to access the current application.

>>> import smisk
>>> smisk.app
None
>>> import smisk.core
>>> smisk.core.Application()
<smisk.core.Application object at 0x6a5c0>
>>> smisk.app
<smisk.core.Application object at 0x6a5c0>
New in version 1.1.0.
smisk.core.request

Current Request (None if no application is running). An ObjectProxy.

New in version 1.1.0.

smisk.core.response

Current Response (None if no application is running). An ObjectProxy.

New in version 1.1.0.

Functions

smisk.core.bind(path[, backlog=-1])

Bind to a specific unix socket or host (and/or port).

Parameterspath (string) — Unix domain socket, hostname, or "host:port"
backlog (int) — listen queue depth; negative = let the system decide
Raisessmisk.IOError if already bound; IOError if socket creation fails
Seeunbind(), listening()
smisk.core.unbind()

Unbind from a previous call to bind(). If not bound, calling this function has no effect.

New in version 1.1.0.

smisk.core.listening()

Find out if this process is bound to a socket via bind(). Returns the bound address/path, or None if not bound.

Raisessmisk.IOError on failure
ReturnsBound path/address or None
smisk.core.uid(nbits[, node=None]) → string

Generate a universally Unique Identifier. The UID is calculated as: sha1(time.secs, time.usecs, pid, random[, node])

NoteThis is not a UUID (RFC 4122) implementation, but uses a similar algorithm. The output format is more compact than UUID v5.
Parametersnbits (int) — bits to pack per byte (4–6), or 0 for 20 raw bytes
node (string) — optional extra data for uid generation
New in version 1.1.0.
smisk.core.pack(data[, nbits=5]) → string

Pack arbitrary bytes into a printable ASCII string. nbits controls encoding density: 0=raw 20B, 4=base16 40B, 5=base32 32B, 6=base64 27B.

Seeuid()
New in version 1.1.0.
smisk.core.object_hash(object) → long

Calculate a hash from any Python object.

New in version 1.1.0.

Classes

class smisk.core.Application

An application. Simple example:

from smisk.core import Application
class MyApp(Application):
  def service(self):
    self.response.write('<h1>Hello World!</h1>')

MyApp().run()
current

Current application instance. Class attribute. See smisk.core.app.

forks

Number of child processes to fork. Set before calling run(). Defaults to 0.

New in version 1.1.0.

request_class

Must be set before calling run().

response_class

Must be set before calling run().

sessions_class

Must be set before calling run(). Should implement the smisk.session.Store interface.

request

The Request object.

response

The Response object.

sessions

An object with the smisk.session.Store interface.

show_traceback

If True, traceback info is included with error responses. Always logged regardless. Defaults to True. Disable in production.

application_did_stop()

Called when the application stops accepting requests. Default does nothing.

application_will_start()

Called just before the application starts accepting requests. Default does nothing.

error(typ, val, tb)

Handle an error and produce an appropriate response. The built-in implementation renders XHTML with HTTP 500. You may override this to customise error responses.

Parameterstyp — Exception type
val — Exception value
tb — Traceback
exit()

Exit application.

run()

Run application.

service()

Service a request. Override in subclasses.

class smisk.core.Request

A HTTP request.

input

Input stream (Stream). Provides raw access to the POST body.

env

HTTP transaction environment (dict).

url

Reconstructed URL.

get

Query string parameters (dict).

post

POST body parameters (dict).

files

Uploaded files from POST (dict).

cookies

Cookies attached to the request (dict).

session

Current session. Modifications must be done before output has begun (will add a Set-Cookie: header).

session_id

Current session id (str).

is_active

Indicates if we are in the middle of an HTTP transaction (bool).

referring_url

Referring URL.

New in version 1.1.0.

method

HTTP method ("GET", "POST", etc.).

SeeRFC 2616, HTTP 1.1, Method Definitions
Typestr
New in version 1.1.1.
max_multipart_size

Limits multipart POST data parsing. -1 disables the limit; 0 disables automatic parsing. Default: 2 GB.

New in version 1.1.2.

max_formdata_size

Limits x-www-form-urlencoded POST data. 0 disables parsing. Cannot be fully disabled (contrast with max_multipart_size). Default: 10 MB.

New in version 1.1.2.

log_error(message)

Log through error stream, including process name and id.

class smisk.core.Response

A HTTP response.

headers

Response headers (list).

out

Output stream (Stream).

has_begun

True if begin() has been called and output has started. Read-only (bool).

__call__(*strings)

Respond with a series of byte strings. Equivalent to writelines(strings). Calls begin() if not yet called.

send_file(path)

Send a file using the host server sendfile-header technique.

begin()

Begin response — send headers. Automatically called by write() and Application.run().

write(str)

Write str bytes to out. Calls begin() if needed.

writelines(lines)

Write a sequence of byte strings to the output stream.

find_header(name) → int

Find a header by name or prefix (case-insensitive). Returns index in headers or -1.

Set a cookie.

Parameters version (int) — Cookie spec version; for RFC 2109, Version=1 applies
max_age (int) — Lifetime in seconds; calculated per the HTTP/1.1 spec; 0 = discard immediately
http_only (bool) — If True, inaccessible to scripts, helping reduce XSS attacks
class smisk.core.URL

Uniform Resource Locator

scheme

The URL schema, always lower case (string).

host

Hostname (string).

port

Port number, 0 if unknown (int).

path

Path component (string).

query

Query string (string).

fragment

Fragment identifier (string).

__init__(obj) → URL

Parse obj as a URL string, or shallow-copy if obj is already a URL.

to_s(scheme=True, user=True, ...) → str

String representation. Pass False for any component to omit it.

static encode(s) → basestring

URL-encode unsafe or reserved characters for use in path, query, or fragment.

static escape(s) → basestring

Escape unsafe characters for safe rendering in URL contexts.

static decode(s) → basestring

Restore data previously encoded by encode() or escape().

static decompose_query(string, charset='utf-8') → dict

Parse a query string into a dictionary, decoding percent-encoding.

class smisk.core.Stream

A file-like I/O stream connected to the host server.

class smisk.core.SessionStore(object)

Basic session store type.

ttl

Session validity duration in seconds. Defaults to 900 (int).

name

Cookie name for the session id. Defaults to "SID" (string).

class smisk.core.FileSessionStore(SessionStore)

File-based session store. See SessionStore.

file_prefix

Prepended to each session file. Defaults to tempfile.tempdir + "smisk-sess."

gc_probability

Probability (0–1) that sessions are garbage-collected on a read. Default 0.1 (10%).

New in version 1.1.0.

read(session_id) → data

Raises InvalidSessionError if session not found.

write(session_id, data)

Write data for session.

destroy(session_id)

Delete a session.

Exceptions

exception smisk.core.Error

Base exception for smisk.core.

exception smisk.core.IOError

I/O error from the C core.

exception smisk.core.InvalidSessionError

Raised when a session cannot be found.

Modules