Python
Send an authenticated event using the template example.
Prepare the environment
Set LOGSTASH_URL and LOGSTASH_PASSWORD as described in Sending data. Run inside the same Railway environment as Logstash.
Example
Source: examples/python/send.py in the template repository. The code below is identical to that file.
"""Send one JSON event with standard-library HTTP and Basic auth."""
import base64
import json
import os
import sys
import urllib.request
class NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
return None
url = os.environ.get("LOGSTASH_URL")
password = os.environ.get("LOGSTASH_PASSWORD")
if not url or not password:
sys.exit("Set LOGSTASH_URL and LOGSTASH_PASSWORD")
event = {"message": "hello from Python", "service": "example-python",
"level": "info", "attributes": {"language": "python"}}
auth = base64.b64encode(f"shipper:{password}".encode()).decode()
request = urllib.request.Request(url, data=json.dumps(event).encode(), headers={
"Content-Type": "application/json", "Authorization": f"Basic {auth}"},
method="POST")
try:
with urllib.request.build_opener(NoRedirect).open(request, timeout=10) as response:
if not 200 <= response.status < 300:
raise RuntimeError("Intake rejected event")
print("Event accepted. Confirm indexing in Kibana.")
except Exception:
sys.exit("Send failed. Check connectivity and intake credentials.")Run and verify
From the template repository:
python3 examples/python/send.pySearch service : "example-python" in Kibana's elk-logs data view. Widen the time range if needed. The program's timeout is configured in the source above. A successful exit confirms acceptance; finding the event confirms indexing.
The template CI uses Node 22, Python 3.12, and Go 1.23. These are selected test runtimes, not minimum supported versions. The Node example requires built-in fetch and AbortSignal.timeout.
Failure behavior
Missing variables, rejected credentials, redirects, and connection errors cause failure. There is no automatic retry. A retry after a timeout may duplicate an event. Do not log the password when debugging.