import socket import os #from test import update_image # device's IP address SERVER_HOST = "0.0.0.0" #(socket.gethostbyname(socket.gethostname())) SERVER_PORT = 5002 # receive 4096 bytes each time BUFFER_SIZE = 4096 SEPARATOR = "" # create the server socket # TCP socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # bind the socket to our local address s.bind((SERVER_HOST, SERVER_PORT)) # enabling our server to accept connections # 5 here is the number of unaccepted connections that # the system will allow before refusing new connections s.listen(5) print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}") # accept connection if there is any client_socket, address = s.accept() # if below code is executed, that means the sender is connected print(f"[+] {address} is connected.") # receive the file infos # receive using client socket, not server socket received = client_socket.recv(BUFFER_SIZE).decode() filename, filesize = received.split(SEPARATOR) # remove absolute path if there is #filename = os.path.basename(filename) # convert to integer filesize = int(filesize) # start receiving the file from the socket # and writing to the file stream #with open(filename, "wb") as f: while True: # read 1024 bytes from the socket (receive) bytes_read = client_socket.recv(BUFFER_SIZE) if not bytes_read: # nothing is received # file transmitting is done take another time break # write to the file the bytes we just received try: hello = bytes_read.decode() #update_image() os.remove("screen.jpg") os.rename(r"screen_download.jpg",r"screen.jpg") print("I am moving the file") except: f = open("screen_download.jpg", 'ab') f.write(bytes_read) print("I am adding to the file") f.close() # close the client socket #client_socket.close() # close the server socket #s.close()