Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import tqdm
- import os
- host, port = "0.0.0.0", 8080
- address = host, port
- try:
- with socket.socket() as s:
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- s.bind(address)
- s.listen()
- while True:
- sock, addr = s.accept()
- print("Connected:", addr)
- filename = "Received.jpg"
- if os.path.exists(filename):
- print(f"File '{filename}' already exists. Choose a different name.")
- sock.close()
- continue
- filesize = int(sock.recv(1024).decode())
- progress = tqdm.tqdm(total=filesize, desc=f"Receiving {filename}", unit="B", unit_scale=True, unit_divisor=1024)
- with open(filename, "wb") as file:
- buffer_size = 8192 # Choose a suitable buffer size
- while True:
- bytes_received = sock.recv(buffer_size)
- if not bytes_received:
- print("Received Successfully")
- break
- file.write(bytes_received)
- progress.update(len(bytes_received))
- except Exception as e:
- print(f"An error occurred: {e}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement