-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.py
More file actions
42 lines (40 loc) · 1.44 KB
/
Copy pathclient_test.py
File metadata and controls
42 lines (40 loc) · 1.44 KB
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
# from ipaddress import IPv4Address
# from dns.inet import AF_INET
import socket
message='/connect'
request_data='A103000F000F'
bytes_req_data=bytes.fromhex(request_data)
#crc=[2D, 6D]
try:
#create socket
client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#connect to server
client_socket.connect(('127.0.0.1',12345))
print("Client is connected")
message = input("Want to send request?(y/n) or '/exit': ")
if message == '/exit':
# close socket
print("Client Disconnected")
client_socket.close()
elif message=='y' or message=='yes':
client_socket.send(bytes_req_data) # sending
client_socket.send(message.encode()) # sending
# receive response
response = client_socket.recv(1024).decode() # receiving
print(f"Server response: {response}")
while True:
# send message
message = input("Another request?(y/n) or '/exit': ")
if message =='/exit':
#close socket
print("Client Disconnected")
client_socket.close()
break
elif message == 'y' or message == 'yes':
client_socket.send(bytes_req_data) # sending
client_socket.send(message.encode()) # sending
# receive response
response = client_socket.recv(1024).decode() # receiving
print(f"Server response: {response}")
except Exception as er:
print(f"Error: {er}")