From 5b893f3e10bd1d40631568652bdef69f087828c7 Mon Sep 17 00:00:00 2001 From: "Christian W. Zuckschwerdt" Date: Thu, 1 Aug 2024 19:59:11 +0200 Subject: [PATCH] Add open in browser script (#2158) --- examples/open_rtl433.py | 91 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 examples/open_rtl433.py diff --git a/examples/open_rtl433.py b/examples/open_rtl433.py new file mode 100755 index 000000000..f38f56179 --- /dev/null +++ b/examples/open_rtl433.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 + +"""Helper command for rtl_433 to visualize a sample file in a web browser.""" + +from http.server import BaseHTTPRequestHandler, HTTPServer +import sys +import subprocess +import webbrowser + +hostName = "localhost" +serverPort = 8080 + + +def parseToPulseData(filename): + ret = subprocess.run(["rtl_433", "-F", "null", "-w", "OOK:-", filename], capture_output=True) + return ret.stdout + + +class PulseServer(BaseHTTPRequestHandler): + def do_GET(self): + global pulsedata + + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + + self.wfile.write(bytes(""" + + + + + + + + + + + + + + + + + + I/Q Spectrogram & Pulsedata + + + + +
+ + + + +""", "utf-8")) + + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage:\n%s FILENAME.cu8" % (sys.argv[0])) + exit(1) + + filename = sys.argv[1] + pulsedata = parseToPulseData(filename) + + while serverPort < 65536: + try: + webServer = HTTPServer((hostName, serverPort), PulseServer) + break; + except OSError as e: + if e.errno != 48 or serverPort >= 65535: + raise + serverPort += 1 # Address already in use + + print("If the browser doesn't open go to http://%s:%s" % (hostName, serverPort)) + + try: + webbrowser.open("http://%s:%s/" % (hostName, serverPort)) + webServer.handle_request() # once + except KeyboardInterrupt: + pass + + webServer.server_close() + print("done.")