kyanit.httpsrv
module
This module is a minimal HTTP server implementation.
Here's a simple example in code.py
, which renders the text Hello from Kyanit!
on
the URL <Kyanit IP>/page
(where <Kyanit IP>
is the IP address of the Kyanit board):
from kyanit import controls, runner, httpsrv
def render_page(method, loc, params, headers, conn, addr):
return httpsrv.response(200, 'Hello from Kyanit!')
@controls()
def main():
http_server = httpsrv.HTTPServer(80)
http_server.register('GET', '^/page$', render_page)
runner.create_task('httpsrv', http_server.catch_requests)
@controls()
def cleanup(exception):
pass
See the HTTPServer
class and module function documentations for details on usage.
Functions
def add_status(num, status_str)
-
By default only
200 OK
,404 Not Found
and500 Internal Server Error
HTTP status codes are available inkyanit.httpsrv
.You may extend this by adding statuses with this function, where
num
is the status code number (int) andstatus_str
is the string. Example:add_status(204, 'No Content')
You are responsible for making the added statuses compliant to HTTP specifications. For a list of compliant status codes, see: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
def add_symbol(perc_enc, symbol)
-
This function extends percent-encoded symbols in a URL. By default only the reserved characters and space are un-encoded. (See https://en.wikipedia.org/wiki/Percent-encoding )
Extend the supported characters with this function where
perc_enc
is the percent- encoded representation andsymbol
is the encoded character. You are responsible for making the encoded symbols compliant to HTTP specifications. def error_view(exc)
-
Return an HTTP 500 response with a JSON body of the error detail.
This function may be monkey-patched to override the default error response. It must return a dict assembled by
response()
. def readall_from(source, into=None, timeout=None, chunk_size=64)
-
This function can be used to read from a socket or file-like object into another socket or file-like object.
timeout
is only relevant for socket objects. def response(status, body='', content_type='text/plain', headers={})
-
Return a response dictionary.
This function should be used from within a callback after assembling the response status, body and headers.
def send_response(conn, status, body='', content_type='text/plain', headers={})
-
This function can be used to send an HTTP response to the
conn
socket.It can be useful for only sending status and headers (leaving the body empty), then the body can be sent by writing to
conn
directly. def unencode(string)
-
This function accepts a string with percent-encoded characters and returns the unencoded string.
Classes
class HTTPServer (port)
-
Methods
async def catch_requests(self)
def close(self)
def deregister(self, method, location_re)
def get_registered(self)
async def processor(self, conn, addr)
def register(self, method, location_re, callback)
def set_timeout(self, timeout)
class NoCallbackError (...)
-
Raised when no callbacks are registered for the requested method and URL. This error is passed to
error_view()
with no arguments.Ancestors
- builtins.Exception
- builtins.BaseException
class NoMethodError (...)
-
Raised when the method requested is not registered to any callbacks. This error is passed to
error_view()
with no arguments.It may be used by a custom
error_view()
function to return a Not Implemented response.Ancestors
- builtins.Exception
- builtins.BaseException
class URLInvalidError (...)
-
Raised when the URL is not valid.
NOTE: Only ASCII characters are supported in the URL.
Ancestors
- builtins.Exception
- builtins.BaseException