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.
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.
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:
source env/bin/activate
Install the required Python packages with:
pip3 install -r requirements.txt
Quickstart (see below for flags, or use -h):
python3 app.py
Stop with Ctrl+C.
Make sure you have Docker installed locally:
docker -v
Quickstart:
make build && make run
Alternatively, build the image locally with:
docker build --tag emoji-app:latest .
Or:
make build
Then, run the container locally with:
docker run --rm --name emoji-app -p 3001:3001/udp emoji-app:latest
Or:
make run
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:
nc -u 0.0.0.0 3001
Send a message and hit enter:
2 :ok:
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)
- Manuel Zander
Many thanks to julvo for his review and great suggestions during development of this software 😊