random_client.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python3
  2. import paho.mqtt.client as paho
  3. import random
  4. import sys
  5. import time
  6. # This is a client that carries out randomised behaviour. It is intended for
  7. # use with the local config file. This file has multiple listeners configured:
  8. # * 1883 - unencrypted MQTT over TCP with no authentication
  9. # * 1884 - unencrypted MQTT over TCP with password authentication
  10. # * 1885 - unencrypted MQTT over TCP with plugin authentication
  11. # * 1886 - unencrypted MQTT over TCP with password and plugin authentication
  12. #
  13. # * 8883 - encrypted MQTT over TCP with no authentication
  14. # * 8884 - encrypted MQTT over TCP with password authentication
  15. # * 8885 - encrypted MQTT over TCP with plugin authentication
  16. # * 8886 - encrypted MQTT over TCP with password and plugin authentication
  17. #
  18. # * 8000 - unencrypted MQTT over WebSockets with no authentication
  19. # * 8001 - unencrypted MQTT over WebSockets with password authentication
  20. # * 8002 - unencrypted MQTT over WebSockets with plugin authentication
  21. # * 8003 - unencrypted MQTT over WebSockets with password and plugin authentication
  22. #
  23. # * 4430 - encrypted MQTT over WebSockets with no authentication
  24. # * 4431 - encrypted MQTT over WebSockets with password authentication
  25. # * 4432 - encrypted MQTT over WebSockets with plugin authentication
  26. # * 4433 - encrypted MQTT over WebSockets with password and plugin authentication
  27. #
  28. # The client randomly picks:
  29. # * A port out of the list
  30. # * Whether to use encryption
  31. # * Whether to use WebSockets
  32. # * Clean start or not
  33. # * Session expiry interval or not
  34. # * QoS to use when subscribing - topics "outgoing/[client id]/message" and "response/#"
  35. # * Lifetime of connection
  36. # On a per publish message basis it chooses:
  37. # * QoS of message
  38. # * Topic of message "outgoing/[0-max client]/message"
  39. # * Retain
  40. # * Interval until next outgoing message
  41. ports = [
  42. {"port":1883, "tls":False, "transport":"tcp", "auth":False},
  43. {"port":1884, "tls":False, "transport":"tcp", "auth":True},
  44. {"port":1885, "tls":False, "transport":"tcp", "auth":True},
  45. {"port":1886, "tls":False, "transport":"tcp", "auth":True},
  46. {"port":8883, "tls":True, "transport":"tcp", "auth":False},
  47. {"port":8884, "tls":True, "transport":"tcp", "auth":True},
  48. {"port":8885, "tls":True, "transport":"tcp", "auth":True},
  49. {"port":8886, "tls":True, "transport":"tcp", "auth":True},
  50. {"port":8000, "tls":False, "transport":"websockets", "auth":False},
  51. {"port":8001, "tls":False, "transport":"websockets", "auth":True},
  52. {"port":8002, "tls":False, "transport":"websockets", "auth":True},
  53. {"port":8003, "tls":False, "transport":"websockets", "auth":True},
  54. {"port":4430, "tls":True, "transport":"websockets", "auth":False},
  55. {"port":4431, "tls":True, "transport":"websockets", "auth":True},
  56. {"port":4432, "tls":True, "transport":"websockets", "auth":True},
  57. {"port":4433, "tls":True, "transport":"websockets", "auth":True},
  58. ]
  59. booleans = [True, False]
  60. qos_values = [0, 1, 2]
  61. def on_connect(client, userdata, flags, rc):
  62. global running
  63. if rc == 0:
  64. client.subscribe("response/#", subscribe_qos)
  65. client.subscribe("outgoing/%s/message" % (client_id), subscribe_qos)
  66. else:
  67. running = False
  68. def on_message(client, userdata, msg):
  69. pass
  70. def on_publish(client, userdata, mid):
  71. pass
  72. def on_disconnect(client, userdata, rc):
  73. running = False
  74. def do_publish(client):
  75. retain = random.choice(booleans)
  76. qos = random.choice(qos_values)
  77. topic = "outgoing/%d/message" % (random.uniform(1, 1000))
  78. payload = "message"
  79. client.publish(topic, payload, qos, retain)
  80. next_publish_time = time.time() + random.uniform(0.1, 2.0)
  81. def main():
  82. global running
  83. global lifetime
  84. mqttc = paho.Client(client_id, clean_session=clean_start, protocol=protocol, transport=transport)
  85. mqttc.on_message = on_message
  86. mqttc.on_publish = on_publish
  87. mqttc.on_connect = on_connect
  88. mqttc.on_disconnect = on_disconnect
  89. if auth and random.choice(booleans):
  90. if random.choice(booleans):
  91. mqttc.username_pw_set("test", "password")
  92. else:
  93. mqttc.username_pw_set("bad", "bad")
  94. if use_tls:
  95. mqttc.tls_set(ca_certs="../ssl/all-ca.crt")
  96. mqttc.connect("localhost", port)
  97. mqttc.loop_start()
  98. while running:
  99. time.sleep(0.1)
  100. now = time.time()
  101. if now > next_publish_time:
  102. do_publish(mqttc)
  103. if now > lifetime:
  104. if random.choice(booleans):
  105. mqttc.disconnect()
  106. lifetime += 5.0
  107. else:
  108. running = False
  109. p = random.choice(ports)
  110. port = p["port"]
  111. use_tls = p["tls"]
  112. transport = p["transport"]
  113. auth = p["auth"]
  114. client_id = "cid"+sys.argv[1]
  115. clean_start = random.choice(booleans)
  116. subscribe_qos = random.choice(qos_values)
  117. protocol = paho.MQTTv311
  118. next_publish_time = time.time() + random.uniform(0.1, 2.0)
  119. lifetime = time.time() + random.uniform(5.0, 10.0)
  120. running = True
  121. main()