✳simplemagic / data
Developers/Pulse
On this page

Ingest and query from Python

This recipe uses Python's standard library and a running standalone node. It is an HTTP example, not a published Pulse Python SDK. Complete the quickstart first.

Use one bounded request helper

import json
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen

BASE_URL = "http://127.0.0.1:9070"

def request(path, body, content_type):
    req = Request(BASE_URL + path, data=body,
                  headers={"Content-Type": content_type})
    with urlopen(req, timeout=10) as response:
        payload = response.read()
        return response.status, json.loads(payload) if payload else None

The socket timeout is a client waiting bound. A timed-out write may still finish on the server; the helper intentionally does not retry mutations.

Write a point

point = b"temperature,device=python-demo value=23.5 1791504002000000000"
try:
    status, _ = request("/write", point, "text/plain")
    assert status == 204
except HTTPError as error:
    detail = error.read().decode("utf-8", errors="replace")
    print("Write rejected or uncertain:", error.code, detail)
    raise
except (URLError, TimeoutError):
    # A missing response does not prove that the write failed.
    print("Write outcome unknown; reconcile before resubmitting")
    raise

Do not concatenate untrusted identifiers or values directly into ILP. Apply the protocol's escaping and type rules in an application serializer. This fixed example avoids that concern by using literal values.

Query the point

sql = "SELECT device, value FROM temperature WHERE device = 'python-demo'"
status, result = request(
    "/query", json.dumps({"sql": sql}).encode("utf-8"), "application/json"
)
print(result)

JSON encoding protects the request structure; it does not parameterize SQL. Avoid interpolating user-supplied text into SQL. For a simple typed filter, use the scan endpoint with a JSON equals object.

Integrate deliberately

Bound batch size and in-flight requests. Retain enough application identity to reconcile uncertain writes. Track write failures separately from query failures. Keep the endpoint private and move to the correct authenticated deployment mode before exposing it to a network.

Search documentation

Type to search across both databases.

↑ ↓ to navigate · Enter to openEsc to close