-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
300a683
commit 66a579d
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
examples/examples-communication/http-server/python-post-server/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
48 changes: 48 additions & 0 deletions
48
examples/examples-communication/http-server/python-post-server/server.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |