Skip to content

Commit 66a579d

Browse files
committed
chunked posts
1 parent 300a683 commit 66a579d

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

examples/examples-communication/http-client/streams-http_post/streams-http_post.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
const char *ssid = "your SSID";
1313
const char *password = "your PASSWORD";
14-
const char *url_str = "http://192.168.1.44:9999";
14+
const char *url_str = "http://192.168.1.44:9988";
1515
AudioInfo info(44100, 2, 16);
1616
SineWaveGenerator<int16_t> sineWave(32000);
1717
GeneratedSoundStream<int16_t> sound(sineWave);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
This directory contains a server in Python that was used to test the [Arduino
3+
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.
4+
5+
The server logs each written line and writes the data to a file.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
#!/usr/bin/env python3
3+
4+
from http.server import HTTPServer, SimpleHTTPRequestHandler
5+
6+
HOST = ""
7+
PORT = 9988
8+
path = "./audio.pcm"
9+
10+
class TestHTTPRequestHandler(SimpleHTTPRequestHandler):
11+
def do_POST(self):
12+
self.send_response(200)
13+
self.end_headers()
14+
15+
if "Content-Length" in self.headers:
16+
content_length = int(self.headers["Content-Length"])
17+
body = self.rfile.read(content_length)
18+
with open(path, "wb") as out_file:
19+
print("writing:", content_length)
20+
out_file.write(body)
21+
elif "chunked" in self.headers.get("Transfer-Encoding", ""):
22+
with open(path, "wb") as out_file:
23+
while True:
24+
line = self.rfile.readline().strip()
25+
print(line)
26+
chunk_length = int(line, 16)
27+
28+
if chunk_length != 0:
29+
print("writing chunk:", chunk_length)
30+
chunk = self.rfile.read(chunk_length)
31+
out_file.write(chunk)
32+
33+
# Each chunk is followed by an additional empty newline
34+
# that we have to consume.
35+
self.rfile.readline()
36+
37+
# Finally, a chunk size of 0 is an end indication
38+
if chunk_length == 0:
39+
break
40+
41+
def main():
42+
httpd = HTTPServer((HOST, PORT), TestHTTPRequestHandler)
43+
print("Serving at port:", httpd.server_port)
44+
httpd.serve_forever()
45+
46+
47+
if __name__ == "__main__":
48+
main()

0 commit comments

Comments
 (0)