29 lines
761 B
Python
29 lines
761 B
Python
import socket
|
|
|
|
def receive_file(port):
|
|
# Create a UDP socket
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
sock.bind(('', port))
|
|
print(f"Listening for incoming files on port {port}...")
|
|
|
|
# Receive file information
|
|
data, addr = sock.recvfrom(1024)
|
|
filename, filesize = data.decode().split(":")
|
|
filesize = int(filesize)
|
|
|
|
with open(filename, "wb") as f:
|
|
print(f"Receiving {filename}...")
|
|
bytes_received = 0
|
|
|
|
while bytes_received < filesize:
|
|
data, addr = sock.recvfrom(1024) # Receive in chunks
|
|
f.write(data)
|
|
bytes_received += len(data)
|
|
|
|
print(f"File {filename} received successfully.")
|
|
sock.close()
|
|
|
|
if __name__ == "__main__":
|
|
port = int(input("Enter the port to listen on: "))
|
|
receive_file(port)
|