domo/domo_node/test.py

37 lines
1.1 KiB
Python
Raw Normal View History

2023-10-15 18:06:34 +02:00
import socket
import sys
# Create a UDP socket
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send_file_contents(filename):
try:
print(f"reading {filename}...")
with open(filename, 'rb') as file:
file_contents = file.read()
except FileNotFoundError:
print(f"File '{filename}' not found.")
return
print(f"sending content len: {len(file_contents)}...")
# Send the file contents to the UDP socket
udp_socket.sendto(file_contents, ('0.0.0.0', 4480))
print("waiting for response...")
# Receive and display the response
response, server_address = udp_socket.recvfrom(1024)
print(f"Received response from {server_address}:")
print(response.hex())
if __name__ == "__main__":
try:
while True:
filename = input("Enter a file name (or 'exit' to quit): ")
if filename == 'exit':
break
send_file_contents(filename)
except KeyboardInterrupt:
print("\nExiting the program.")
finally:
udp_socket.close()