Skip to content

Commit e3f0f31

Browse files
committed
examples/http_server*: Update for buffered-like streams (read line by line).
Since "read-exactly" stream refactor, where stream.read(N) will read exactly N bytes (unless EOF), http_server* examples can't any longer do client_socket.read(4096) and expect to get full request (it will block on HTTP/1.1 client). Instead, read request line by line, as the HTTP protocol requires.
1 parent 1459a8d commit e3f0f31

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

examples/network/http_server.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ def main(use_stream=False):
3434
if use_stream:
3535
# MicroPython socket objects support stream (aka file) interface
3636
# directly.
37-
print(client_s.read(4096))
37+
req = client_s.readline()
38+
print(req)
39+
while True:
40+
h = client_s.readline()
41+
if h == b"" or h == b"\r\n":
42+
break
43+
print(h)
3844
client_s.write(CONTENT % counter)
3945
else:
4046
print(client_s.recv(4096))

examples/network/http_server_ssl.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ def main(use_stream=True):
4242
# next request they issue will likely be more well-behaving and
4343
# will succeed.
4444
try:
45-
req = client_s.read(4096)
45+
req = client_s.readline()
4646
print(req)
47+
while True:
48+
h = client_s.readline()
49+
if h == b"" or h == b"\r\n":
50+
break
51+
print(h)
4752
if req:
4853
client_s.write(CONTENT % counter)
4954
except Exception as e:

0 commit comments

Comments
 (0)