File tree Expand file tree Collapse file tree 3 files changed +54
-1
lines changed
examples/examples-communication
http-client/streams-http_post
http-server/python-post-server Expand file tree Collapse file tree 3 files changed +54
-1
lines changed Original file line number Diff line number Diff line change 1111
1212const char *ssid = " your SSID" ;
1313const 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 " ;
1515AudioInfo info (44100 , 2 , 16 );
1616SineWaveGenerator<int16_t > sineWave (32000 );
1717GeneratedSoundStream<int16_t > sound (sineWave);
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments