feat: debugging server update
This commit is contained in:
parent
b456391e62
commit
a108ce5226
1 changed files with 36 additions and 9 deletions
|
@ -1,18 +1,45 @@
|
||||||
|
|
||||||
import socket
|
import socket, zlib
|
||||||
|
|
||||||
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
|
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
|
||||||
PORT = 2000 # Port to listen on (non-privileged ports are > 1023)
|
PORT = 2000 # Port to listen on (non-privileged ports are > 1023)
|
||||||
|
|
||||||
|
def calculate_crc(data):
|
||||||
|
# Calculate the CRC32 checksum of the data
|
||||||
|
crc = zlib.crc32(data)
|
||||||
|
# Convert the CRC32 value to a 4-byte big-endian byte array
|
||||||
|
crc_bytes = crc.to_bytes(4, byteorder='big')
|
||||||
|
return crc_bytes
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
s.bind((HOST, PORT))
|
s.bind((HOST, PORT))
|
||||||
s.listen()
|
s.listen()
|
||||||
while True:
|
while True:
|
||||||
conn, addr = s.accept()
|
try:
|
||||||
with conn:
|
conn, addr = s.accept()
|
||||||
print(f"Connected by {addr}")
|
with conn:
|
||||||
while True:
|
print(f"Connected by {addr}")
|
||||||
data = conn.recv(1024)
|
while True:
|
||||||
if not data:
|
data = conn.recv(1024)
|
||||||
break
|
if not data:
|
||||||
print(f'{data.hex()} [{len(data)}]')
|
break
|
||||||
|
print(f'RECV: {data.hex()} [{len(data)}]')
|
||||||
|
if data[0x11] == 0x01:
|
||||||
|
pckt = bytearray([0x01])
|
||||||
|
pckt.extend([0x00, 0x00, 0x00, 0x00]) # src
|
||||||
|
pckt.extend([0x00, 0x00, 0x00, 0xFF]) # dest
|
||||||
|
pckt.extend([0x00, 0x00, 0x00, 0x00]) # packet_id
|
||||||
|
pckt.extend(data[0x09:0x0D]) # reply_to
|
||||||
|
pckt.extend([0x01]) # command
|
||||||
|
pckt.extend([0x00, 0x04]) # data_length
|
||||||
|
pckt.extend([0x00, 0x00, 0x00, 0xFF])
|
||||||
|
|
||||||
|
# Calculate CRC for the received data
|
||||||
|
crc = calculate_crc(pckt)
|
||||||
|
# Append the CRC value to the data
|
||||||
|
data_with_crc = pckt + crc
|
||||||
|
# Send the modified data (with CRC) back to the client
|
||||||
|
conn.sendall(data_with_crc)
|
||||||
|
print(f'SEND: {data_with_crc.hex()} [{len(data_with_crc)}]')
|
||||||
|
except:
|
||||||
|
s.close()
|
Loading…
Reference in a new issue