From c5e4915f5f20938efcbe0bb568a346f96ab17e79 Mon Sep 17 00:00:00 2001 From: Pingu Date: Thu, 16 Mar 2023 10:53:18 +0100 Subject: [PATCH] Now sends a message to ntfy on boot --- main.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..ffd9e64 --- /dev/null +++ b/main.py @@ -0,0 +1,49 @@ +import usocket as socket +import ssl + + +def main(): + s = socket.socket() + + host = "ntfy.acorneroftheweb.com" + token = "" + port = 443 + + ai = socket.getaddrinfo(host, port) + print("Address infos:", ai) + addr = ai[0][-1] + + print("Connect address:", addr) + s.connect(addr) + + s = ssl.wrap_socket(s) + + headers = """\ +POST / HTTP/1.1\r +Host: {host}\r +Content-Type: application/json\r +Authorization: Bearer {token} +Accept: */*\r +Content-Length: {content_length}\r +\r\n""" + body = '{"topic": "d1", "message": "Hejsan", "title": "Dab"}' + + + body_bytes = body.encode('ascii') + header_bytes = headers.format( + content_length=len(body_bytes), + token=token, + host=str(host) + ).encode('iso-8859-1') + + payload = header_bytes + body_bytes + + print(payload) + + s.write(payload) + print(s.read(4096)) + + s.close() + + +main()