[熱情]

python mqtt client 본문

Programming/python

python mqtt client

rootkaien 2016. 8. 18. 12:11
https://pypi.python.org/pypi/paho-mqtt/1.2


Installation


The latest stable version is available in the Python Package Index (PyPi) and can be installed using

pip install paho-mqtt

Or with virtualenv:

virtualenv paho-mqtt
source paho-mqtt/bin/activate
pip install paho-mqtt


To obtain the full code, including examples and tests, you can clone the git repository:

git clone https://github.com/eclipse/paho.mqtt.python

Once you have the code, it can be installed from your repository as well:

cd paho.mqtt.python

python setup.py install



import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("hello/world")

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))


# broker로 부터 메시지를 받는다.
if __name__ == '__main__':
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect("iot.eclipse.org", 1883, 60)

    client.loop_forever() 


Comments