Like @ognqn.chikov said you can do this with the MQTT API. I have an example here with directions on using it with Python.