Write in a socket without reading the incoming data
Posted: Tue May 24, 2022 8:18 am
Is it possible to answer a request without reading the whole incoming data?
I want to verify if the path is correct in the code below. The request never gets responded to (it gets an Error: read ECONNRESET), but it works if I read all the incoming data before writing.
Thank you!
I want to verify if the path is correct in the code below. The request never gets responded to (it gets an Error: read ECONNRESET), but it works if I read all the incoming data before writing.
Code: Select all
import uasyncio as asyncio
server = None
async def run():
server = await asyncio.start_server(run_client, '0.0.0.0', 80, 1)
while True:
await asyncio.sleep(100)
async def run_client(sreader, swriter):
# res = await asyncio.wait_for(sreader.read(4000), 5) # This works
first = await uasyncio.wait_for(sreader.readline(), 10)
if b"/my_desired_path" in first:
swriter.write("HTTP/1.0 200 NA\r\n\r\n")
else:
swriter.write("HTTP/1.0 400 NA\r\n\r\n")
await swriter.drain()
await swriter.wait_closed()
await sreader.wait_closed()
print('Client socket closed.')
async def close():
print('Closing server')
server.close()
await server.wait_closed()
print('Server closed.')
try:
asyncio.run(run())
except KeyboardInterrupt:
print('Interrupted')
finally:
asyncio.run(close())
_ = asyncio.new_event_loop()