Asyncio Echo server, unwanted messages #15397
-
Following an example TCP echo client and TCP echo server at: This part I can get to work, but I think there might be something that Watching the server in rshell:
(CTRL-C, CTRL-D in rshell seems to provoke the issue)
I tried to find if there was a way to flush those ' ' messages from I can ignore these messages or do a machine.reset(), The relevant part on the server: async def handle_echo(reader, writer):
while True:
try:
data = await reader.read(100)
message = data.decode()
addr = writer.get_extra_info('peername')
print(f"Received {message!r} from {addr!r}")
except:
print("Close the connection")
writer.close()
await writer.wait_closed()
# if message is '' do NOT process it, kludge!
if message is not '':
print(f"Received {message!r}")
if message == 'ping':
if config.watchdog is True:
wdt_feed()
wdt.feed()
print(f"Send: {message!r}")
writer.write(data)
await writer.drain()
await asyncio.sleep(1)
async def main():
print('awaiting client connection')
server = await asyncio.start_server(
handle_echo, config.server, config.port)
# async with server:
# await server.serve_forever()
async with server:
while True:
await asyncio.sleep(300)
# connect to hotspot
connect()
asyncio.run(main()) The "problem" also occurs when the system is working and the I would like to know why I get these unwanted messages and how to get |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 18 replies
-
Thought this might be useful: Could be a more complex issue then I can handle: Edit: I am fairly happy for now. |
Beta Was this translation helpful? Give feedback.
-
dave, |
Beta Was this translation helpful? Give feedback.
-
I will get a chance to try your suggested improvements in a few days. That was a poor Thank you for taking the time to make helpful suggestions ... I have lots to learn in the |
Beta Was this translation helpful? Give feedback.
-
This is how i deal with those:
you can remove the print lines or put the connection message after that i do check header data for the content length and use |
Beta Was this translation helpful? Give feedback.
StreamReader.read()
will return b"" on EOF, ie. if the connection is closed from the remote or the connection is lost.You could just cleanup and exit the
handle_echo()
function when you receive an emptyread()
, eg:I believe that closing the writer should be the correct way to cleanup and ensure the connection is fully closed, but it may be unnecessary.
It looks from your second output printout above that you have…