Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
镜像
Eclipse Foundation
paho.mqtt.python
提交
6df2e791
P
paho.mqtt.python
项目概览
镜像
/
Eclipse Foundation
/
paho.mqtt.python
10 个月 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
paho.mqtt.python
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6df2e791
编写于
7月 07, 2021
作者:
R
Roger A. Light
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Test and documentation for decorators.
上级
d8aa1c18
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
146 addition
and
0 deletion
+146
-0
ChangeLog.txt
ChangeLog.txt
+1
-0
src/paho/mqtt/client.py
src/paho/mqtt/client.py
+41
-0
test/lib/01-decorators.py
test/lib/01-decorators.py
+56
-0
test/lib/Makefile
test/lib/Makefile
+1
-0
test/lib/python/01-decorators.test
test/lib/python/01-decorators.test
+47
-0
未找到文件。
ChangeLog.txt
浏览文件 @
6df2e791
...
...
@@ -11,6 +11,7 @@ v1.6.0 - 2021-xx-xx
the server. They will *not* be retried when the client is still connected.
- The `rc` parameter in the `on_disconnect` callback now has meaningful values
in the case of an error. Closes #441.
- Callbacks can now be applied to client instances using decorators.
v1.5.1 - 2020-09-22
...
...
src/paho/mqtt/client.py
浏览文件 @
6df2e791
...
...
@@ -437,6 +437,14 @@ class Client(object):
client.on_connect = on_connect
Callbacks can also be attached using decorators:
client = paho.mqtt.Client()
@client.connect_callback()
def on_connect(client, userdata, flags, rc, properties=None):
print("Connection returned " + str(rc))
All of the callbacks as described below have a "client" and an "userdata"
argument. "client" is the Client instance that is calling the callback.
"userdata" is user data of any type and can be set when creating a new client
...
...
@@ -464,6 +472,9 @@ class Client(object):
5: Connection refused - not authorised
6-255: Currently unused.
Decorator: @client.connect_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_disconnect(client, userdata, rc): called when the client disconnects from the broker.
The rc parameter indicates the disconnection state:
MQTT_ERR_SUCCESS: the callback was called in response to a disconnect() call.
...
...
@@ -476,10 +487,16 @@ class Client(object):
message can contain a reason code and MQTT V5 properties. The properties parameter could be
None if they do not exist in the disconnect message.
Decorator: @client.disconnect_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_message(client, userdata, message): called when a message has been received on a
topic that the client subscribes to. The message variable is a
MQTTMessage that describes all of the message parameters.
Decorator: @client.message_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_publish(client, userdata, mid): called when a message that was to be sent using the
publish() call has completed transmission to the broker. For messages
with QoS levels 1 and 2, this means that the appropriate handshakes have
...
...
@@ -489,34 +506,58 @@ class Client(object):
This callback is important because even if the publish() call returns
success, it does not always mean that the message has been sent.
Decorator: @client.publish_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_subscribe(client, userdata, mid, granted_qos, properties=None): called when the broker responds to a
subscribe request. The mid variable matches the mid variable returned
from the corresponding subscribe() call. The granted_qos variable is a
list of integers that give the QoS level the broker has granted for each
of the different subscription requests.
Decorator: @client.subscribe_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_unsubscribe(client, userdata, mid): called when the broker responds to an unsubscribe
request. The mid variable matches the mid variable returned from the
corresponding unsubscribe() call.
Decorator: @client.unsubscribe_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_log(client, userdata, level, buf): called when the client has log information. Define
to allow debugging. The level variable gives the severity of the message
and will be one of MQTT_LOG_INFO, MQTT_LOG_NOTICE, MQTT_LOG_WARNING,
MQTT_LOG_ERR, and MQTT_LOG_DEBUG. The message itself is in buf.
Decorator: @client.log_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_socket_open(client, userdata, sock): Called when the socket has been opened. Use this
to register the socket with an external event loop for reading.
Decorator: @client.socket_open_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_socket_close(client, userdata, sock): Called when the socket is about to be closed.
Use this to unregister a socket from an external event loop for reading.
Decorator: @client.socket_close_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_socket_register_write(client, userdata, sock): Called when a write operation to the
socket failed because it would have blocked, e.g. output buffer full. Use this to
register the socket with an external event loop for writing.
Decorator: @client.socket_register_write_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_socket_unregister_write(client, userdata, sock): Called when a write operation to the
socket succeeded after it had previously failed. Use this to unregister the socket
from an external event loop for writing.
Decorator: @client.socket_unregister_write_callback() (```client``` is the name of the
instance which this callback is being attached to)
"""
def
__init__
(
self
,
client_id
=
""
,
clean_session
=
None
,
userdata
=
None
,
...
...
test/lib/01-decorators.py
0 → 100755
浏览文件 @
6df2e791
#!/usr/bin/env python3
# Test whether callback decorators work
import
context
import
paho_test
rc
=
1
keepalive
=
60
connect_packet
=
paho_test
.
gen_connect
(
"decorators-test"
,
keepalive
=
keepalive
)
connack_packet
=
paho_test
.
gen_connack
(
rc
=
0
)
subscribe_packet
=
paho_test
.
gen_subscribe
(
mid
=
1
,
topic
=
u
"sub-test"
,
qos
=
1
)
suback_packet
=
paho_test
.
gen_suback
(
mid
=
1
,
qos
=
1
)
unsubscribe_packet
=
paho_test
.
gen_unsubscribe
(
mid
=
2
,
topic
=
u
"unsub-test"
)
unsuback_packet
=
paho_test
.
gen_unsuback
(
mid
=
2
)
publish_packet
=
paho_test
.
gen_publish
(
u
"b2c"
,
qos
=
0
,
payload
=
"msg"
.
encode
(
'utf-8'
))
publish_packet_in
=
paho_test
.
gen_publish
(
u
"decorators"
,
qos
=
1
,
mid
=
3
,
payload
=
"message"
.
encode
(
'utf-8'
))
puback_packet_in
=
paho_test
.
gen_puback
(
mid
=
3
)
disconnect_packet
=
paho_test
.
gen_disconnect
()
sock
=
paho_test
.
create_server_socket
()
client
=
context
.
start_client
()
try
:
(
conn
,
address
)
=
sock
.
accept
()
conn
.
settimeout
(
10
)
if
paho_test
.
expect_packet
(
conn
,
"connect"
,
connect_packet
):
conn
.
send
(
connack_packet
)
if
paho_test
.
expect_packet
(
conn
,
"subscribe"
,
subscribe_packet
):
conn
.
send
(
suback_packet
)
if
paho_test
.
expect_packet
(
conn
,
"unsubscribe"
,
unsubscribe_packet
):
conn
.
send
(
unsuback_packet
)
conn
.
send
(
publish_packet
)
if
paho_test
.
expect_packet
(
conn
,
"publish"
,
publish_packet_in
):
conn
.
send
(
puback_packet_in
)
if
paho_test
.
expect_packet
(
conn
,
"disconnect"
,
disconnect_packet
):
rc
=
0
conn
.
close
()
finally
:
client
.
terminate
()
client
.
wait
()
sock
.
close
()
exit
(
rc
)
test/lib/Makefile
浏览文件 @
6df2e791
...
...
@@ -6,6 +6,7 @@ PYTHON?=python3
all
:
test
:
$(PYTHON)
./01-decorators.py python/01-decorators.test
$(PYTHON)
./01-keepalive-pingreq.py python/01-keepalive-pingreq.test
$(PYTHON)
./01-no-clean-session.py python/01-no-clean-session.test
$(PYTHON)
./01-reconnect-on-failure.py python/01-reconnect-on-failure.test
...
...
test/lib/python/01-decorators.test
0 → 100755
浏览文件 @
6df2e791
#!/usr/bin/env python3
import
os
import
socket
import
subprocess
import
sys
import
time
from
struct
import
*
import
paho
.
mqtt
.
client
as
mqtt
run
=
-
1
mqttc
=
mqtt
.
Client
(
"decorators-test"
,
run
)
payload
=
b
""
@
mqttc
.
connect_callback
()
def
on_connect
(
mqttc
,
obj
,
flags
,
rc
)
:
mqttc
.
subscribe
(
"sub-test"
,
1
)
@
mqttc
.
subscribe_callback
()
def
on_subscribe
(
mqttc
,
obj
,
mid
,
granted_qos
)
:
mqttc
.
unsubscribe
(
"unsub-test"
)
@
mqttc
.
unsubscribe_callback
()
def
on_unsubscribe
(
mqttc
,
obj
,
mid
)
:
global
payload
payload
=
"message"
@
mqttc
.
message_callback
()
def
on_message
(
mqttc
,
obj
,
msg
)
:
global
payload
mqttc
.
publish
(
"decorators"
,
qos
=
1
,
payload
=
payload
)
@
mqttc
.
publish_callback
()
def
on_publish
(
mqttc
,
obj
,
mid
)
:
mqttc
.
disconnect
()
@
mqttc
.
disconnect_callback
()
def
on_disconnect
(
mqttc
,
obj
,
rc
)
:
obj
=
rc
mqttc
.
connect
(
"localhost"
,
1888
)
while
run
==
-
1
:
mqttc
.
loop
()
exit
(
run
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录