dnsmasq.py:
Suggest you use
ps = subprocess.Popen("ps -e | grep ' dnsmasq' | awk '{print $1}'", shell=True, stdout=subprocess.PIPE)
instead of
ps = subprocess.Popen("ps -e | grep ' dnsmasq' | cut -c 1-7", shell=True, stdout=subprocess.PIPE)
The latter can truncate the PID