skirl

Python

A few lines send a message from a Python script, with Python's standard library only: nothing to install.

Setting it up#

In the app this code is under Connect a system → Python, or a topic's Send to this topic tab, with your address, the topic and a token filled in; New token makes the token.

Standard library only: nothing to install.

import urllib.request

request = urllib.request.Request(
    "https://notify.example.com/scripts",
    data="pve-01 → offsite. 14 VMs, no errors.".encode(),
    headers={
        "Authorization": "Bearer tk_replace_with_your_token",
        "Title": "Nightly backup finished",
    },
)
urllib.request.urlopen(request, timeout=10)

Skirl's tests run this code as the app shows it, with no packages at all. The message is sent when urlopen runs, which waits up to 10 seconds for Skirl.

When Skirl refuses the message, or cannot be reached, urlopen raises an error and the script stops there. If the script should carry on, catch it:

import urllib.error

try:
    urllib.request.urlopen(request, timeout=10)
except urllib.error.URLError as e:
    print("Skirl did not take the message:", e)

For a priority or tags, add them to headers, such as "Priority": "4" and "Tags": "warning": Title, priority and tags. A text of 4096 bytes or more arrives as a file attached to the message: Attachments.

What a message looks like#

Nightly backup finished pve-01 → offsite. 14 VMs, no errors.

Priority 3

When nothing arrives#

The error Python raises names the reason:

urllib.error.HTTPError: HTTP Error 401: Unauthorized
  • 401 Unauthorized: the token is wrong, revoked or expired. Make a new one with New token, or Rotate, and paste it in again.
  • 403 Forbidden: the token may not send to this topic: it is limited to another one, the topic name is misspelled, or there is no token in the request.
  • 404 Not Found: the address is not a topic's: your server's address, a / and the topic name, nothing after it.
  • 413: the text or file is larger than this server takes. 429 Too Many Requests: too many messages too fast, or too many failed sign-ins from this address: Rate limits.
  • urllib.error.URLError with no status (a name that cannot be found, a refused connection, a timeout): this machine cannot reach the address.
  • CERTIFICATE_VERIFY_FAILED: Python does not trust the server's certificate. A server with a certificate from your own authority needs that authority's file named in SSL_CERT_FILE when the script runs.

An HTTPError carries Skirl's own explanation: e.read().decode() gives it, such as {"code":40301,"http":403,"error":"forbidden"}. The general checks: A sender gets no message through. Every error code: Error messages.

This page on GitHub