Grole API

Grole is a python (3.5+) nano web framework based on asyncio. It’s goals are to be simple, embedable (single file and standard library only) and easy to use.

class grole.Grole(env={})[source]

Bases: object

A Grole Webserver

__init__(env={})[source]

Initialise a server

env is passed to request handlers to provide shared state. Note, env by default contains doc which is populated from registered route docstrings.

route(path_regex, methods=['GET'], doc=True)[source]

Decorator to register a handler

Parameters:
  • path_regex: Request path regex to match against for running the handler
  • methods: HTTP methods to use this handler for
  • doc: Add to internal doc structure
run(host='localhost', port=1234, ssl_context=None)[source]

Launch the server. Will run forever accepting connections until interrupted.

Parameters:

  • host: The host to listen on
  • port: The port to listen on
  • ssl_context: The SSL context passed to asyncio
class grole.Request[source]

Bases: object

Represents a single HTTP request

The following members are populated with the request details:

  • method: The request method
  • location: The request location as it is sent
  • path: The unescaped path part of the location
  • query: The query string part of the location (if present)
  • version: The request version, e.g. HTTP/1.1
  • headers: Dictionary of headers from the request
  • data: Raw data from the request body
  • match: The re.MatchObject from the successful path matching
body()[source]

Decodes body as string

json()[source]

Decodes json object from the body

class grole.Response(data=None, code=200, reason='OK', headers={}, version='HTTP/1.1')[source]

Bases: object

Represents a single HTTP response

__init__(data=None, code=200, reason='OK', headers={}, version='HTTP/1.1')[source]

Create a response

Parameters:

  • data: Object to send e.g. ResponseBody / ResponseJSON.
  • code: The response code, default 200
  • reason: The response reason, default OK
  • version: The response version, default HTTP/1.1
  • headers: Dictionary of response headers, default is a Server header and those from the response body

Note, data is intelligently converted to an appropriate ResponseXYZ object depending on it’s type.

class grole.ResponseBody(data=b'', content_type='text/plain')[source]

Bases: object

Response body from a byte string

__init__(data=b'', content_type='text/plain')[source]

Initialise object, data is the data to send

Parameters:

  • data: Byte data to send
  • content_type: Value of Content-Type header, default text/plain
class grole.ResponseFile(filename, content_type=None)[source]

Bases: grole.ResponseBody

Respond with a file

Content type is guessed if not provided

__init__(filename, content_type=None)[source]

Initialise object, data is the data to send

Parameters:

  • filename: Name of file to read and send
  • content_type: Value of Content-Type header, default is to guess from file extension
class grole.ResponseJSON(data='', content_type='application/json')[source]

Bases: grole.ResponseString

Response body encoded in json

__init__(data='', content_type='application/json')[source]

Initialise object, data is the data to send

Parameters:

  • data: Object to encode as json for sending
  • content_type: Value of Content-Type header, default application/json
class grole.ResponseString(data='', content_type='text/html')[source]

Bases: grole.ResponseBody

Response body from a string

__init__(data='', content_type='text/html')[source]

Initialise object, data is the data to send

Parameters:

  • data: String data to send
  • content_type: Value of Content-Type header, default text/plain
grole.main(args=['-T', '-b', 'readthedocs', '-d', '_build/doctrees-readthedocs', '-D', 'language=en', '.', '_build/html'])[source]

Run Grole static file server

grole.parse_args(args=['-T', '-b', 'readthedocs', '-d', '_build/doctrees-readthedocs', '-D', 'language=en', '.', '_build/html'])[source]

Parse command line arguments for Grole server running as static file server

grole.serve_doc(app, url)[source]

Serve API documentation extracted from request handler docstrings

Parameters:
  • app: Grole application object
  • url: URL to serve at
grole.serve_static(app, base_url, base_path, index=False)[source]

Serve a directory statically

Parameters:

  • app: Grole application object
  • base_url: Base URL to serve from, e.g. /static
  • base_path: Base path to look for files in
  • index: Provide simple directory indexes if True