Server And Client
Learn how to implement a network server using asyncio in Python.
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 usingaiohttp
. However, it is not that useful in most cases because it will often be slower than a fast, optimized, native WSGI server likeuwsgi
orgunicorn
. 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 ...