HostHttpServer

class stencila.HostHttpServer(host, address='127.0.0.1', port=2000)[source]

A HTTP server for a Host

Provides access to a Host via a REST-like HTTP protocol using POST to create new instance and PUT to run one of it’s methods. Implements authorization using single-, or multi-, use “tickets” and session tokens.

The following example illustrates creating a PythonContext and then running it’s execute method. It uses the http command line tool (https://httpie.org/) for brevity and session management but you could also use curl or other HTTP client.

# Start the server
> python -m stencila
Host has started at: http://127.0.0.1:2000/?ticket=w8Z0ZkuWlz8Y
Use Ctrl+C to stop

# Then in another shell create a new PythonContext (using the above ticket
# to obtain access authorization and a session token) using POST
> http --session=/tmp/session.json  POST :2000/PythonContext?ticket=w8Z0ZkuWlz8Y
HTTP/1.0 200 OK
Content-Length: 21
Content-Type: application/json
Date: Wed, 28 Feb 2018 21:36:37 GMT
Server: Werkzeug/0.12.2 Python/2.7.12
Set-Cookie: token=PjvskQ38vtuJQg2hNYEHwPppvw8RKbs0AaYcA9uStannZkGfRr3I0g9jyeQD3L3f; Path=/

"pythonContext1"

# Then use the returned name of the PythonContext instance to run it's "execute" method
# using PUT
> http --session=/tmp/session.json  PUT :2000/pythonContext1!execute code='sys.version'
HTTP/1.0 200 OK
Content-Length: 153
Content-Type: application/json
Date: Wed, 28 Feb 2018 21:39:54 GMT
Server: Werkzeug/0.12.2 Python/2.7.12

{
    "messages": [],
    "value": {
        "data": "2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609]",
        "type": "string"
    }
}
url

Get the URL of the server

Returns:A URL string
start(real=True)[source]

Start the server

stop(real=True)[source]

Stop the server

handle(request)[source]

Handle a HTTP request

route(verb, path, authorized=False)[source]

Route a HTTP request

static(request, response, path)[source]

Handle a GET request for a static file

run(request, response, method, *args)[source]

Run a host method

error(request, response, code, name, what='')[source]
error400(request, response, what='')[source]
error401(request, response, what='')[source]
error403(request, response, what='')[source]
error404(request, response, what='')[source]
error500(request, response)[source]