all Python 3until 3.12Removed from the standard libraryterm
asynchat
removed modules
Layer over asyncore for command/response protocols split on a terminator. Removed in Python 3.12; use asyncio streams instead.
Syntax
import asynchat # до 3.12
asynchat.async_chat buffered incoming bytes and called found_terminator() when it met a given terminator such as b'\r\n'; that is how line-based protocol servers were written. The module sat on top of asyncore and left with it: deprecated in Python 3.6, removed in 3.12 under PEP 594. How to rewrite: set_terminator() and collect_incoming_data() become one line, line = await reader.readuntil(b'\r\n'); reply with writer.write(...) and await writer.drain(). Connections come from asyncio.start_server() and asyncio.open_connection(). The pyasynchat package on PyPI brings the module back, but asyncio is the recommended path.
Examples
# Python 3.12+
try:
import asynchat
except ModuleNotFoundError as e:
print(type(e).__name__) # → ModuleNotFoundError
# Python 3.12+
import asyncio, socket
async def main():
a, b = socket.socketpair()
reader, writer = await asyncio.open_connection(sock=a)
peer_reader, peer_writer = await asyncio.open_connection(sock=b)
peer_writer.write(b'HELLO\r\nBYE\r\n')
await peer_writer.drain()
print(await reader.readuntil(b'\r\n')) # → b'HELLO\r\n'
print(await reader.readuntil(b'\r\n')) # → b'BYE\r\n'
writer.close()
peer_writer.close()
asyncio.run(main())
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy