파이썬에서 포트 8080으로 mqtt 브로커를 구독하도록 지원합니까?
import sys
import paho.mqtt.client as mqtt
def on_connect(mqttc, obj, flags, rc):
print("rc: "+str(rc))
def on_message(mqttc, obj, msg):
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
def on_publish(mqttc, obj, mid):
print("mid: "+str(mid))
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_log(mqttc, obj, level, string):
print(string)
mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect("test.mosquitto.org", 8080, 60)
mqttc.subscribe("test/iot", 0)
mqttc.loop_forever()
이 코드로 연결할 수 없습니다. Mosquitto는 포트 8080에서 웹 소켓을 지원하지만이 paho 라이브러리는 작동하지 않습니다. 파이썬에 대한 해결책은 무엇입니까? Windows 10에서 Python 2.7을 사용하고 있습니다.
Paho MQTT 모듈은 며칠 전에 웹 소켓 지원을 도입했습니다. 아직 릴리스되지 않은 것 같지만 Linux 브랜치의 마스터에서 다음을 사용하여 설치할 수 있습니다.
pip install git+git://github.com/eclipse/paho.mqtt.python.git
창문 아래에서도 작동합니다. (댓글의 정보에 감사드립니다)
다음과 연결하여 웹 소켓을 전송으로 사용할 수 있습니다.
mqttc = mqtt.Client(transport="websockets")
최신 정보:
브라우저 클라이언트 (예 : MQTT.js)도 연결해야하기 때문에 python 클라이언트와 함께 websocket 프로토콜을 사용하려는 경우 웹 소켓 및 일반 mqtt 프로토콜 을 수신하도록 mosquitto를 구성 할 수도 있습니다 .
예를 들어 구성 파일을 생성하십시오.
/etc/mosquitto/mosquitto.conf
다음 내용으로 :
listener 1883
protocol mqtt
listener 9001
protocol websockets
그런 다음 모기를 실행할 수 있습니다.
mosquitto -c /etc/mosquitto/mosquitto.conf
유사한 출력이 표시되어야합니다.
1469015320: mosquitto version 1.4.8 (build date 2016-05-3112:07:40+0200) starting
1469015320: Config loaded from /etc/mosquitto/mosquitto1.conf.
1469015320: Opening ipv4 listen socket on port 1883.
1469015320: Opening ipv6 listen socket on port 1883.
1469015320: Opening websockets listen socket on port 9001.
그런 다음 Python 클라이언트는 포트 1883에 연결하고 브라우저 클라이언트는 9001에 연결합니다.
what-mqtt 브라우저 클라이언트 를 사용하여 websocket 리스너를 테스트 할 수 있습니다 . 그냥 가리켜ws://localhost:9001
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다