-
Notifications
You must be signed in to change notification settings - Fork 727
/
Copy pathclient_pub_opts.py
executable file
·165 lines (131 loc) · 4.22 KB
/
client_pub_opts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2017 Jon Levell <[email protected]>
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Distribution License v1.0
# which accompanies this distribution.
#
# The Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# All rights reserved.
# This shows a example of an MQTT publisher with the ability to use
# user name, password CA certificates based on command line arguments
import argparse
import os
import ssl
import time
import paho.mqtt.client as mqtt
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--host", required=False, default="mqtt.eclipseprojects.io")
parser.add_argument("-t", "--topic", required=False, default="paho/test/opts")
parser.add_argument("-q", "--qos", required=False, type=int, default=0)
parser.add_argument("-c", "--clientid", required=False, default=None)
parser.add_argument("-u", "--username", required=False, default=None)
parser.add_argument(
"-d",
"--disable-clean-session",
action="store_true",
help="disable 'clean session' (sub + msgs not cleared when client disconnects)",
)
parser.add_argument("-p", "--password", required=False, default=None)
parser.add_argument(
"-P",
"--port",
required=False,
type=int,
default=None,
help="Defaults to 8883 for TLS or 1883 for non-TLS",
)
parser.add_argument(
"-N",
"--nummsgs",
required=False,
type=int,
default=1,
help="send this many messages before disconnecting",
)
parser.add_argument(
"-S",
"--delay",
required=False,
type=float,
default=1,
help="number of seconds to sleep between msgs",
)
parser.add_argument("-k", "--keepalive", required=False, type=int, default=60)
parser.add_argument("-s", "--use-tls", action="store_true")
parser.add_argument("--insecure", action="store_true")
parser.add_argument("-F", "--cacerts", required=False, default=None)
parser.add_argument(
"--tls-version",
required=False,
default=None,
help="TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1\n",
)
parser.add_argument("-D", "--debug", action="store_true")
args, unknown = parser.parse_known_args()
def on_connect(mqttc, obj, flags, rc):
print("connect 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)
usetls = args.use_tls
if args.cacerts:
usetls = True
port = args.port
if port is None:
if usetls:
port = 8883
else:
port = 1883
mqttc = mqtt.Client(args.clientid, clean_session=not args.disable_clean_session)
if usetls:
if args.tls_version == "tlsv1.2":
tlsVersion = ssl.PROTOCOL_TLSv1_2
elif args.tls_version == "tlsv1.1":
tlsVersion = ssl.PROTOCOL_TLSv1_1
elif args.tls_version == "tlsv1":
tlsVersion = ssl.PROTOCOL_TLSv1
elif args.tls_version is None:
tlsVersion = None
else:
print("Unknown TLS version - ignoring")
tlsVersion = None
if not args.insecure:
cert_required = ssl.CERT_REQUIRED
else:
cert_required = ssl.CERT_NONE
mqttc.tls_set(
ca_certs=args.cacerts,
certfile=None,
keyfile=None,
cert_reqs=cert_required,
tls_version=tlsVersion,
)
if args.insecure:
mqttc.tls_insecure_set(True)
if args.username or args.password:
mqttc.username_pw_set(args.username, args.password)
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
if args.debug:
mqttc.on_log = on_log
print("Connecting to " + args.host + " port: " + str(port))
mqttc.connect(args.host, port, args.keepalive)
mqttc.loop_start()
for x in range(0, args.nummsgs):
msg_txt = '{"msgnum": "' + str(x) + '"}'
print("Publishing: " + msg_txt)
infot = mqttc.publish(args.topic, msg_txt, qos=args.qos)
infot.wait_for_publish()
time.sleep(args.delay)
mqttc.disconnect()