platypush.backend.http

class platypush.backend.http.HttpBackend(port=8008, websocket_port=8009, disable_websocket=False, dashboard=None, resource_dirs=None, ssl_cert=None, ssl_key=None, ssl_cafile=None, ssl_capath=None, maps=None, run_externally=False, uwsgi_args=None, **kwargs)[source]

The HTTP backend is a general-purpose web server that you can leverage:

  • To execute Platypush commands via HTTP calls. Example:

    curl -XPOST -H 'Content-Type: application/json' -H "X-Token: your_token" \
        -d '{
            "type":"request",
            "target":"nodename",
            "action":"tts.say",
            "args": {"phrase":"This is a test"}
        }' \
        http://localhost:8008/execute
    
  • To interact with your system (and control plugins and backends) through the Platypush web panel, by default available on your web root document. Any plugin that you have configured and available as a panel plugin will appear on the web panel as well as a tab.

  • To display a fullscreen dashboard with your configured widgets, by default available under /dashboard

  • To stream media over HTTP through the /media endpoint

Any plugin can register custom routes under platypush/backend/http/app/routes/plugins. Any additional route is managed as a Flask blueprint template and the .py module can expose lists of routes to the main webapp through the __routes__ object (a list of Flask blueprints).

Note that if you set up a main token, it will be required for any HTTP interaction - either as X-Token HTTP header, on the query string (attribute name: token), as part of the JSON payload root (attribute name: token), or via HTTP basic auth (any username works).

Requires:

  • flask (pip install flask)
  • redis (pip install redis)
  • websockets (pip install websockets)
  • python-dateutil (pip install python-dateutil)
  • magic (pip install python-magic), optional, for MIME type
    support if you want to enable media streaming
  • uwsgi (pip install uwsgi plus uwsgi server installed on your
    system if required) - optional but recommended. By default the Platypush web server will run in a process spawned on the fly by the HTTP backend. However, being a Flask app, it will serve clients in a single thread and won’t support many features of a full-blown web server.

Base command to run the web server over uwsgi:

uwsgi --http :8008 --module platypush.backend.http.uwsgi --master --processes 4 --threads 4

Bear in mind that the main webapp is defined in platypush.backend.http.app:application and the WSGI startup script is stored under platypush/backend/http/uwsgi.py.

__init__(port=8008, websocket_port=8009, disable_websocket=False, dashboard=None, resource_dirs=None, ssl_cert=None, ssl_key=None, ssl_cafile=None, ssl_capath=None, maps=None, run_externally=False, uwsgi_args=None, **kwargs)[source]
Parameters:
  • port (int) – Listen port for the web server (default: 8008)
  • websocket_port (int) – Listen port for the websocket server (default: 8009)
  • disable_websocket (bool) – Disable the websocket interface (default: False)
  • ssl_cert (str) – Set it to the path of your certificate file if you want to enable HTTPS (default: None)
  • ssl_key (str) – Set it to the path of your key file if you want to enable HTTPS (default: None)
  • ssl_cafile (str) – Set it to the path of your certificate authority file if you want to enable HTTPS (default: None)
  • ssl_capath (str) – Set it to the path of your certificate authority directory if you want to enable HTTPS (default: None)
  • resource_dirs (dict[str, str]) – Static resources directories that will be accessible through /resources/<path>. It is expressed as a map where the key is the relative path under /resources to expose and the value is the absolute path to expose.
  • dashboard – Set it if you want to use the dashboard service. It will contain the configuration for the widgets to be used (look under platypush/backend/http/templates/widgets/ for the available widgets).

Example configuration:

dashboard:
    background_image: https://site/image.png
    widgets:                # Each row of the dashboard will have 6 columns
        -
            widget: calendar           # Calendar widget
            columns: 6
        -
            widget: music              # Music widget
            columns: 3
        -
            widget: date-time-weather  # Date, time and weather widget
            columns: 3
        -
            widget: image-carousel     # Image carousel
            columns: 6
            # Absolute path (valid as long as it's a subdirectory of one of the available `resource_dirs`)
            images_path: ~/Dropbox/Photos/carousel
            refresh_seconds: 15
        -
            widget: rss-news           # RSS feeds widget
            # Requires backend.http.poll to be enabled with some RSS sources and write them to sqlite db
            columns: 6
            limit: 25
            db: "sqlite:////home/user/.local/share/platypush/feeds/rss.db"
Parameters:
  • run_externally (bool) – If set, then the HTTP backend will not directly spawn the web server. Set this option if you plan to run the webapp in a separate web server (recommended), like uwsgi or uwsgi+nginx.
  • uwsgi_args (list[str]) –

    If run_externally is set and you would like the HTTP backend to directly spawn and control the uWSGI application server instance, then pass the list of uWSGI arguments through this parameter. Some examples include:

    # Start uWSGI instance listening on HTTP port 8008 with 4
    # processes
    ['--plugin', 'python', '--http-socket', ':8008', '--master', '--processes', '4']
    
    # Start uWSGI instance listening on uWSGI socket on port 3031.
    # You can then use another full-blown web server, like nginx
    # or Apache, to communicate with the uWSGI instance
    ['--plugin', 'python', '--socket', '127.0.0.1:3031', '--master', '--processes', '4']
    
notify_web_clients(event)[source]

Notify all the connected web clients (over websocket) of a new event

on_stop()[source]

On backend stop

run()[source]

Starts the backend thread. To be implemented in the derived classes

send_message(msg, **kwargs)[source]

Sends a platypush.message.Message to a node. To be implemented in the derived classes. By default, if the Redis backend is configured then it will try to deliver the message to other consumers through the configured Redis main queue.

Parameters:
  • msg – The message to send
  • queue_name – Send the message on a specific queue (default: the queue_name configured on the Redis backend)
websocket()[source]

Websocket main server