krait package¶
Module contents¶
This is the root of the Krait API. This has two purposes:
- As a package, this holds the different modules that you can use to interact with Krait,
namely
krait.config
,krait.mvc
,krait.cookie
andkrait.websockets
. - As a (pseudo-)module, this holds generic data related to the HTTP request, or site configuration. While this may not be the best practice, given Python’s poor concurrency, the fact that any process serves at most one request at a time, this is safe. Krait emphasizes ease of use over absolute purity.
Reference¶
-
krait.
site_root
= None¶ str – The site root (lifted directly from the krait argument)
-
krait.
get_full_path
(filename)[source]¶ Convert a filename relative to the site root to its full path. Note that this is not necessarily absolute, but is derived from the krait argument.
Parameters: filename (str) – a filename relative to the site root (the krait argument) Returns: os.path.join(krait.site_root, filename)
Return type: str
-
krait.
request
= None¶ krait.Request
– The HTTP request that is being handled right now. Set by Krait.
-
krait.
response
= None¶ krait.Response
– Set this to completely change the HTTP response, leave None for usual behaviour. Useful when you want, for example, to return a 400 Bad Request or 302 Found. If you only want to add or change headers, usekrait.extra_headers
.
-
class
krait.
Request
(http_method, url, query_string, http_version, headers, body)[source]¶ Bases:
object
Represents an HTTP request. Objects of this class are created by Krait before passing control to your Python code. The variable
krait.request
is an instance of this class.Parameters: - http_method (str) – The HTTP method in the request. Values: ‘GET’, ‘POST’, etc.
- url (str) – The URL of the requests, without the query
- query_string (str) – The URL query
- http_version (str) – the HTTP version; only ‘HTTP/1.1’ is supported
- headers (dict of str str) – The HTTP headers sent by the client
- body (str) – The body of the request
-
http_method
¶ str – The HTTP method in the request. Values: ‘GET’, ‘POST’, etc.
-
url
¶ str – The URL of the requests, without the query
-
query
¶ dict of str str – The query extracted from the URL, parsed in a dict.
-
class
MultipartFormData
(data, name, filename, content_type)¶ Bases:
tuple
-
content_type
¶ Alias for field number 3
-
data
¶ Alias for field number 0
-
filename
¶ Alias for field number 2
-
name
¶ Alias for field number 1
-
-
Request.
get_multipart_form
()[source]¶ Extract an HTTP multipart form from the request body.
Returns: the HTTP form parts. Return type: list of Request.MultipartFormData
-
class
krait.
Response
(http_version, status_code, headers, body)[source]¶ Bases:
object
Represents an HTTP response. Set
krait.response
with an instance of this variable (or its subclasses) to override the HTTP response.Parameters: - http_version (str) – ‘HTTP/1.1’, no other values are supported.
- status_code (int) – The HTTP status code (for example, 200 or 404).
- headers (list of (str, str)) – The response headers.
- body – The response body.
-
http_version
¶ str – The HTTP version of the response.
-
status_code
¶ int – The HTTP status code.
-
headers
¶ list of (str, str) – The response headers.
-
body
¶ The response body.
-
class
krait.
ResponseNotFound
(headers=None)[source]¶ Bases:
krait._http_response.Response
Response returning a 404 Not Found
Parameters: headers (list of (str, str), optional) – Extra headers to send with the response.
-
class
krait.
ResponseBadRequest
(headers=None)[source]¶ Bases:
krait._http_response.Response
Response returning 400 Bad Request
Parameters: headers (list of (str, str), optional) – Extra headers to send with the response.
-
class
krait.
ResponseRedirect
(destination, headers=None)[source]¶ Bases:
krait._http_response.Response
Response returning a 302 Found redirect.
Parameters: - destination (str) – The URL on which to redirect the client.
- headers (list of (str, str), optional) – Extra headers to send with the response.
-
krait.
extra_headers
= None¶ list of (str, str) – Extra response headers to set without overriding the entire response.
-
krait.
set_content_type
(raw=None, ext=None)[source]¶ Set the HTTP Content-Type to a custom value. Used when the original route target’s extension is not relevant to the extension of the final content.
Parameters: - raw (str, optional) – full MIME type (e.g.
'application/json'
) - ext (str, optional) – file extension from which to derive the MIME type (e.g.
'json'
)
- raw (str, optional) – full MIME type (e.g.