-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_client.py
90 lines (75 loc) · 2.73 KB
/
test_client.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
import socket
import time
import argparse
def send_tcp_packets(ip, port, interval):
try:
# Create a TCP/IP socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Connect to the server
sock.connect((ip, port))
print(f"Connected to {ip}:{port}")
while True:
# Send 0x00
sock.sendall(b"\x00")
print("Sent: 0x00")
time.sleep(interval)
# Send 0x01
sock.sendall(b"\x01")
print("Sent: 0x01")
time.sleep(interval)
except Exception as e:
print(f"An error occurred: {e}")
def send_single_tcp_packet(ip, port, data):
try:
# Create a TCP/IP socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Connect to the server
sock.connect((ip, port))
print(f"Connected to {ip}:{port}")
# Send the specified data
sock.sendall(data)
print(f"Sent: {data.hex()}")
# If the command is "get", wait for a response
if data == b"\x03":
response = sock.recv(1024)
print(f"Received: {response.hex()}")
time.sleep(0.1) # Give time for ACK
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
# Set up argument parser
parser = argparse.ArgumentParser(
description="TCP client that sends 0x00 and 0x01 repeatedly or specific commands."
)
parser.add_argument(
"--ip", type=str, required=True, help="The IP address of the TCP server."
)
parser.add_argument(
"--port", type=int, required=True, help="The port number of the TCP server."
)
parser.add_argument(
"--interval",
type=float,
help="Time interval between sending 0x00 and 0x01 packets (in seconds).",
)
parser.add_argument(
"--on", action="store_true", help="Send 0x01 to turn on the device."
)
parser.add_argument(
"--off", action="store_true", help="Send 0x00 to turn off the device."
)
parser.add_argument(
"--get", action="store_true", help="Send 0x03 to get the current state."
)
args = parser.parse_args()
# Check if interval is set, otherwise use single command
if args.interval is not None:
send_tcp_packets(args.ip, args.port, args.interval)
elif args.on:
send_single_tcp_packet(args.ip, args.port, b"\x01")
elif args.off:
send_single_tcp_packet(args.ip, args.port, b"\x00")
elif args.get:
send_single_tcp_packet(args.ip, args.port, b"\x03")
else:
print("Please specify --interval, --on, --off, or --get.")