import socket import time import binascii import threading TARGET_IP = "127.0.0.1" TARGET_PORT = 1883 CLIENT1_HEX = "101e00044d5154548400003c0012636c69656e745f69645f746573745f303031;821700010012746573742f636c65616e5f73657373696f6e01" CLIENT2_HEX = "101b00044d5154548402003c000f6e616e6f6d712d6333636265646635;32360012746573742f636c65616e5f73657373696f6e000168656c6c6f5f746869735f69735f616e5f6f66666c696e655f6d657373616765" def run_client1_subscriber(): print("[C1] Thread Started: Connecting & Subscribing...") try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TARGET_IP, TARGET_PORT)) packets = CLIENT1_HEX.split(';') for hex_pkt in packets: if not hex_pkt: continue s.send(binascii.unhexlify(hex_pkt)) time.sleep(0.01) print("[C1] Subscription sent. Keeping connection open & Waiting for data...") s.settimeout(5.0) while True: try: data = s.recv(1024) if not data: print("[C1] Connection closed by server.") break print(f"[C1] <<< RECEIVED DATA: {binascii.hexlify(data).decode()}") except socket.timeout: print("[C1] Timeout (No more data). Stopping C1.") break s.close() print("[C1] Socket Closed.") except Exception as e: print(f"[C1] Error: {e}") def run_client2_publisher(): time.sleep(1.0) print("\n[C2] Thread Started: Connecting & Publishing...") try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TARGET_IP, TARGET_PORT)) packets = CLIENT2_HEX.split(';') for hex_pkt in packets: if not hex_pkt: continue s.send(binascii.unhexlify(hex_pkt)) time.sleep(0.01) print("[C2] Publish sent. Closing C2.") s.close() except Exception as e: print(f"[C2] Error: {e}") def main(): for i in range(5): print(f"\n[*] Round {i+1}/5") print(f"[*] Target: {TARGET_IP}:{TARGET_PORT}") print("[*] Starting interaction logic...") t1 = threading.Thread(target=run_client1_subscriber) t1.start() run_client2_publisher() t1.join() print("[*] Interaction finished.") if __name__ == "__main__": main()