From 66a579d52f66f821e4ea8415a3569a0e5695cc7d Mon Sep 17 00:00:00 2001 From: pschatzmann Date: Thu, 21 Nov 2024 16:00:05 +0100 Subject: [PATCH] chunked posts --- .../streams-http_post/streams-http_post.ino | 2 +- .../http-server/python-post-server/README.md | 5 ++ .../http-server/python-post-server/server.py | 48 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 examples/examples-communication/http-server/python-post-server/README.md create mode 100755 examples/examples-communication/http-server/python-post-server/server.py diff --git a/examples/examples-communication/http-client/streams-http_post/streams-http_post.ino b/examples/examples-communication/http-client/streams-http_post/streams-http_post.ino index b6eedcc2b..883c8c741 100644 --- a/examples/examples-communication/http-client/streams-http_post/streams-http_post.ino +++ b/examples/examples-communication/http-client/streams-http_post/streams-http_post.ino @@ -11,7 +11,7 @@ const char *ssid = "your SSID"; const char *password = "your PASSWORD"; -const char *url_str = "http://192.168.1.44:9999"; +const char *url_str = "http://192.168.1.44:9988"; AudioInfo info(44100, 2, 16); SineWaveGenerator sineWave(32000); GeneratedSoundStream sound(sineWave); diff --git a/examples/examples-communication/http-server/python-post-server/README.md b/examples/examples-communication/http-server/python-post-server/README.md new file mode 100644 index 000000000..ca24566e9 --- /dev/null +++ b/examples/examples-communication/http-server/python-post-server/README.md @@ -0,0 +1,5 @@ + +This directory contains a server in Python that was used to test the [Arduino +post sketch](https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-communication/http-client/streams-http_post/streams-http_post.ino) using chunged writes. + +The server logs each written line and writes the data to a file. diff --git a/examples/examples-communication/http-server/python-post-server/server.py b/examples/examples-communication/http-server/python-post-server/server.py new file mode 100755 index 000000000..a63e4d1d3 --- /dev/null +++ b/examples/examples-communication/http-server/python-post-server/server.py @@ -0,0 +1,48 @@ + +#!/usr/bin/env python3 + +from http.server import HTTPServer, SimpleHTTPRequestHandler + +HOST = "" +PORT = 9988 +path = "./audio.pcm" + +class TestHTTPRequestHandler(SimpleHTTPRequestHandler): + def do_POST(self): + self.send_response(200) + self.end_headers() + + if "Content-Length" in self.headers: + content_length = int(self.headers["Content-Length"]) + body = self.rfile.read(content_length) + with open(path, "wb") as out_file: + print("writing:", content_length) + out_file.write(body) + elif "chunked" in self.headers.get("Transfer-Encoding", ""): + with open(path, "wb") as out_file: + while True: + line = self.rfile.readline().strip() + print(line) + chunk_length = int(line, 16) + + if chunk_length != 0: + print("writing chunk:", chunk_length) + chunk = self.rfile.read(chunk_length) + out_file.write(chunk) + + # Each chunk is followed by an additional empty newline + # that we have to consume. + self.rfile.readline() + + # Finally, a chunk size of 0 is an end indication + if chunk_length == 0: + break + +def main(): + httpd = HTTPServer((HOST, PORT), TestHTTPRequestHandler) + print("Serving at port:", httpd.server_port) + httpd.serve_forever() + + +if __name__ == "__main__": + main()