Skip to content
Discussion options

You must be logged in to vote

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 empty read(), eg:

async def handle_echo(reader, writer):
    while True:
        try:
            data = await reader.read(100)
            if not data:
                writer.close()
                await writer.closed()
                return
            message = data.decode()
            ...

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…

Replies: 4 comments 18 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
7 replies
@davefes
Comment options

@glenn20
Comment options

Answer selected by davefes
@davefes
Comment options

@glenn20
Comment options

@davefes
Comment options

@glenn20
Comment options

Comment options

You must be logged in to vote
1 reply
@glenn20
Comment options

Comment options

You must be logged in to vote
10 replies
@glenn20
Comment options

@davefes
Comment options

@davefes
Comment options

@glenn20
Comment options

@davefes
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants