I would have gone even further with PostmarketOS: https://wiki.postmarketos.org/wiki/Motorola_Moto_G5(motorola-cedric)
I remember using a poor man’s Python web server on an old Android phone with stock OS (non rooted). I just installed QPython3 app from Google Play, then I ran my Python+Bottle project on it.
Sometimes, the wifi went off for battery efficiency, thus making the server down. I was able to disable this feature so the wifi of the device was always on, as well as the QPython3 process.
The last step was doing the NAT forwarding with the router and I was all set!
It was pretty fun.