kopia lustrzana https://github.com/micropython/micropython
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.pull/2227/merge
rodzic
1459a8d5c9
commit
e3f0f31e07
|
@ -34,7 +34,13 @@ def main(use_stream=False):
|
||||||
if use_stream:
|
if use_stream:
|
||||||
# MicroPython socket objects support stream (aka file) interface
|
# MicroPython socket objects support stream (aka file) interface
|
||||||
# directly.
|
# 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)
|
client_s.write(CONTENT % counter)
|
||||||
else:
|
else:
|
||||||
print(client_s.recv(4096))
|
print(client_s.recv(4096))
|
||||||
|
|
|
@ -42,8 +42,13 @@ def main(use_stream=True):
|
||||||
# next request they issue will likely be more well-behaving and
|
# next request they issue will likely be more well-behaving and
|
||||||
# will succeed.
|
# will succeed.
|
||||||
try:
|
try:
|
||||||
req = client_s.read(4096)
|
req = client_s.readline()
|
||||||
print(req)
|
print(req)
|
||||||
|
while True:
|
||||||
|
h = client_s.readline()
|
||||||
|
if h == b"" or h == b"\r\n":
|
||||||
|
break
|
||||||
|
print(h)
|
||||||
if req:
|
if req:
|
||||||
client_s.write(CONTENT % counter)
|
client_s.write(CONTENT % counter)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Ładowanie…
Reference in New Issue