I'm trying to download a file from target computer remotely while having reverse connection. I am using Linux as my server machine and my target machine (client) is Windows 10. My code is written in python3.9. Before modifying my code I was able to download files, however I would lose connection as soon as entered wrong file path. So now I modified the code to first confirm if the path given exists or not. So now I'm able to keep my connection with the client alive and if I enter wrong file path it does show the error message, however the problem is that now I'm not able to download the file by proving the correct path. Now when I enter correct path nothing happens, the cursor just blinks. Can I know what's wrong in either side of my code and the correction and also how to maintain the connection incase of wrong input, as at the moment I lose connection with the client side whenever I enter something wrong.
Here's my server side code:
while True:
#BUFFER_SIZE=1024
# get the command from prompt
command = input("Command >> ")
if command == "":
continue
# download file
#THIS IS SERVER MACHINE - LINUX
elif command.lower() == "download":
client_socket.send(command.encode())
print("")
filepath = input(str("Please enter the file path: "))
client_socket.send(filepath.encode())
confirm = client_socket.recv(BUFFER_SIZE).decode()
if confirm == "Please enter correct File Path":
print(confirm) #just checking if I'm getting response from client side,but it seems that I dont.
print("")
continue
elif confirm == "Press [y] to Confirm Download": #add hereTOMORO
print(confirm)
inp_confirmation = input(str("Press [y] to Confirm Download: "))
client_socket.send(inp_confirmation.encode())
print("Recieving file... ")
results = client_socket.recv(1078000)
new_file = open(filepath, "wb")
new_file.write(results)
new_file.close()
print("")
print(filepath, "Has been downloaded and saved")
print("")
else:
Err = client_socket.recv(BUFFER_SIZE).decode()
print(Err)
print("")
continue
And Here is my Client side code:
while True:
if command.lower() == "download":
file_path = s.recv(1024).decode()
if os.path.isfile(file_path):
msg2 = "Press [y] to Confirm Download "
s.send(msg2.encode())
confirmation_recv = s.recv(1024).decode()
if recv_confirmation.lower() == "y":
with open(file_path, "rb") as file:
print("sending file... ")
data = file.read(1078000)
s.send(data)
while(data !=""):
data = file.read(1078000)
s.send(data)
file.close()
print("Done Sending!")
else:
msg = "Please enter correct File Path"
s.send(msg.encode())
Thanks!