# Example adapted from: # https://pythontic.com/modules/socket/udp-client-server-example import socket import time localIP = "0.0.0.0" # Server localPort = 22200 bufferSize = 1024 # Create datagram socket UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) # Bind UDPServerSocket.bind((localIP, localPort)) print("UDP Server started on port " + str(localPort)) # Application constants and variables EOF = chr(26) PATH = 'drive\\path_where_received_files_should_be_stored\\' DEBUG = False; # Files loop while(True): file = open(PATH + "temp-" + time.strftime("%Y%m%d.%H%M%S") + ".txt", "a") # Listen for incoming datagrams while(True): bytesAddressPair = UDPServerSocket.recvfrom(bufferSize) message = bytesAddressPair[0].decode('latin1') if DEBUG: print(message) if EOF in message: file.write(message[:len(message)-2]) break else: file.write(message) print("EOF detected.") print("temp-" + time.strftime("%Y%m%d.%H%M%S") + ".txt" + " written.\n") file.close() if DEBUG: break # End files loop quit() # Unreachable!