The client-server model with multiple connections

If we are working with Python version 3.4+, there is a module called selectors, which provides an API for quickly building an object-oriented server based on the I/O primitives. The documentation and an example of this is available at https://docs.python.org/3.7/library/selectors.html.

In this example, we are going to implement a server that controls several connections using the selectors package.

You can find the following code in the tcp_server_selectors.py file:

#!/usr/bin/env python3import selectorsimport typesimport socketselector = selectors.DefaultSelector()def accept_connection(sock):    connection, address = sock.accept()    print('Connection accepted in {}'.format(address)) # We put the ...

Get Learning Python Networking - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.