liveBook · Manning

Socket.sendall

As explained in Python Concurrency with asyncio

socket.sendall is a socket method that transmits all bytes in a given message before returning or raising an error.

socket.sendall writes a byte message to the connected client. Paired with recv, it gives a socket application both directions of communication. section 3.2 section 3.2.1

Echo the received message

After receiving bytes into buffer, call connection.sendall(buffer). A Telnet client that sends testing123 followed by a line ending then receives the same message, creating a basic echo server. section 3.2.1

while buffer[-2:] != b'\r\n':
        data = connection.recv(2)
        if not data:
            break
        else:
            print(f'I got data: {data}!')
            buffer = buffer + data
    print(f"All the data is: {buffer}")
    connection.sendall(buffer)

Inspect the complete socket sequence: receive two-byte chunks until a carriage return and line feed arrive, then send the accumulated buffer. section 3.2.1

How Python Concurrency with asyncio covers it

How deep it goes
Explains the call within a receive loop and echo server, but stops short of asynchronous or multi-client implementation.
Examples
Uses a localhost Telnet session and a buffer containing testing123 followed by a carriage return and line feed.
What it assumes
Assumes a connected socket and received bytes, while establishing how socket applications write data back.
What you can do after
Apply it via Python sockets to return a received byte message to a client.
How it connects
Connects sendall with sockets, recv, buffers, Telnet, and echo servers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.