summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-07-10 23:01:52 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-07-10 23:01:52 +0300
commite3f0f31e07091642a938d7f243f5803588e409b1 (patch)
tree41fcfced25b4110578dc84aa526d3ed31968fdaa
parent1459a8d5c9b29c78da2cf5c7cf3c37ab03b34b8e (diff)
downloadmicropython-e3f0f31e07091642a938d7f243f5803588e409b1.tar.gz
micropython-e3f0f31e07091642a938d7f243f5803588e409b1.zip
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.
-rw-r--r--examples/network/http_server.py8
-rw-r--r--examples/network/http_server_ssl.py7
2 files changed, 13 insertions, 2 deletions
diff --git a/examples/network/http_server.py b/examples/network/http_server.py
index 80dfc5db02..147bf51226 100644
--- a/examples/network/http_server.py
+++ b/examples/network/http_server.py
@@ -34,7 +34,13 @@ def main(use_stream=False):
if use_stream:
# MicroPython socket objects support stream (aka file) interface
# directly.
- print(client_s.read(4096))
+ req = client_s.readline()
+ print(req)
+ while True:
+ h = client_s.readline()
+ if h == b"" or h == b"\r\n":
+ break
+ print(h)
client_s.write(CONTENT % counter)
else:
print(client_s.recv(4096))
diff --git a/examples/network/http_server_ssl.py b/examples/network/http_server_ssl.py
index 04e0913448..9a69ca9d41 100644
--- a/examples/network/http_server_ssl.py
+++ b/examples/network/http_server_ssl.py
@@ -42,8 +42,13 @@ def main(use_stream=True):
# next request they issue will likely be more well-behaving and
# will succeed.
try:
- req = client_s.read(4096)
+ req = client_s.readline()
print(req)
+ while True:
+ h = client_s.readline()
+ if h == b"" or h == b"\r\n":
+ break
+ print(h)
if req:
client_s.write(CONTENT % counter)
except Exception as e: