Search⌘ K
AI Features

Server And Client

Explore how to implement an asynchronous TCP server and client using Python's asyncio framework. Understand key methods like connection_made and data_received, and test the server's performance with basic statistics. This lesson helps you manage thousands of network connections efficiently using event loops.

We'll cover the following...

Since asyncio is exceptional at handling thousands of network connections, it provides a useful framework for implementing network servers. The following example implements a simplistic TCP server, but it is up to you to build any network server you might like.

Note: It is possible to build an asyncio based Web server using aiohttp. However, it is not that useful in most cases because it will often be slower than a fast, optimized, native WSGI server like uwsgi or gunicorn. Beacuse Python Web applications are always using WSGI, it is easy to switch out the WSGI server for a fast and asynchronous one.

Asyncio server

Following is a simple example of a TCP server. This server listens for strings terminated by \n and returns them in upper case.

To run the following application (server), click Run and enter the command python asyncio-server.py.

To change the source code in the playground and run, click Run again after changing the code, ...