From ebfb1de9d340aa39b3a33c72aa4677b564b03bbc Mon Sep 17 00:00:00 2001 From: Nikhil Kumar Singh Date: Tue, 6 Oct 2026 03:34:54 +0530 Subject: [PATCH] docs: add python requests client --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 03bcc56..bc6b8c9 100644 --- a/README.md +++ b/README.md @@ -38,13 +38,59 @@ computer on the same WiFi: curl http://:8080/status curl -X POST http://:8080/send \ -H 'Content-Type: application/json' \ - -d '{"to": "+15551234567", "message": "hello from pc"}' + -d '{"to": "+919876543210", "message": ""}' ``` Device IP and port are shown on screen. Default port 8080, changeable in the app. The server runs as a foreground service and keeps running in the background. +## Python client + +```python +import requests + + +class SmsServerClient: + """HTTP client for the SMS Server app on the phone.""" + + def __init__(self, host: str, port: int = 8080, token: str | None = None) -> None: + self._base = f"http://{host}:{port}" + self._headers = {"Content-Type": "application/json"} + if token is not None: + self._headers["Authorization"] = f"Bearer {token}" + + def status(self) -> dict: + r = requests.get(f"{self._base}/status", headers=self._headers, timeout=10) + r.raise_for_status() + return r.json() + + def log(self) -> dict: + r = requests.get(f"{self._base}/log", headers=self._headers, timeout=10) + r.raise_for_status() + return r.json() + + def send(self, to: str, message: str) -> dict: + r = requests.post( + f"{self._base}/send", + headers=self._headers, + json={"to": to, "message": message}, + timeout=10, + ) + if r.status_code >= 400: + detail = r.json().get("error", r.text) + raise RuntimeError(f"{r.status_code}: {detail}") + return r.json() + + +if __name__ == "__main__": + client = SmsServerClient("192.168.1.111", port=9090) # device IP : port + print(client.status()) + print(client.send("+15551234567", "hello from python")) +``` + +Save as `client.py` and point it at the address shown on the app screen. + ## Notes - Needs a phone with a SIM; messages go through the carrier. @@ -52,4 +98,4 @@ background. ## License -GPL-3.0. See [LICENSE](LICENSE). \ No newline at end of file +GPL-3.0. See [LICENSE](LICENSE). -- knotmirror 0.0.1