GitHub - manuelzander/python_udp_server: Python UDP server using asyncio, plus Dockerfile, Makefile and unittests
This small project comprises a simple Python UDP server plus Dockerfile, as well as a (hopefully) useful Makefile and unittests.
The server makes use of the asyncio and uvloop libraries to allow for fast, asynchronous request handling.
Check out this blog post if you're interested in more performance details.
Prerequisites
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
The code is run and tested with Python 3.7.7 on macOS 10.14.6 and Python 3.8.5 on Ubuntu 18.04.5.
Environment
Clone the repo to your local machine.
Create a virtual environment for Python 3 with:
python3 -m pip install virtualenv
python3 -m virtualenv -p python3 env
Activate the virtual environment with:
Install the required Python packages with:
pip3 install -r requirements.txt
Run the app without Docker
Quickstart (see below for flags, or use -h):
Stop with Ctrl+C.
Run the app with Docker
Make sure you have Docker installed locally:
Quickstart:
Alternatively, build the image locally with:
docker build --tag emoji-app:latest .
Or:
Then, run the container locally with:
docker run --rm --name emoji-app -p 3001:3001/udp emoji-app:latest
Or:
Optionally, the following flags can be used: --n, --r, --s and --h/-h:
--nMultiply number of emojis by n (int, default:1)--rDisable translation from keyword to emoji (bool, default:False)--sSeparator between each emoji (str, default:"")--h/-hSee usage information
For example:
docker run --rm --name emoji-app -p 3001:3001/udp emoji-app:latest --n 2 --r True --s "+"
In another shell session, you can now trigger the endpoint (0.0.0.0:3001), for example using nc:
Send a message and hit enter:
Development and testing
Using the Makefile you can run make <cmd> where <cmd> is one of:
sort-importsto ensure Python imports are in the correct PEP format/orderformatto format Python files usingblacktype-checkto run static type checking usingmypytestto run unittests usingpytestallto run all steps (or just typemake)
Authors
- Manuel Zander
Acknowledgments
Many thanks to julvo for his review and great suggestions during development of this software ๐