yes, that is what I am saying.
however if the client and server are in the same folder, you will not be able to write to a file (already open by server to read from).
So either put the client and the server in separate folders or just create a subfolder e.g.
server
however if the client and server are in the same folder, you will not be able to write to a file (already open by server to read from).
So either put the client and the server in separate folders or just create a subfolder e.g.
received
andf=open('./received/' + filename, 'wb')
server
#SERVER import socket import threading import os def retrieve_file(name, sock): filename=sock.recv(1024) if os.path.isfile(filename): sock.send(("EXISTS" + str(os.path.getsize(filename))).encode()) user_response = sock.recv(1024) user_response=user_response.decode() if user_response.startswith('OK'): with open(filename, 'rb') as f: send_bytes=f.read(1024) sock.send(send_bytes) while send_bytes !='': send_bytes= f.read(1024) sock.send(send_bytes) else: sock.send(b"error, file does not exist") sock.close() def main(): host = "127.0.0.1" port = 5000 s=socket.socket() s.bind((host,port)) s.listen(5) print("server started...") while True: c, addr= s.accept() print("client connected ip>:" + str(addr)) t=threading.Thread(target=retrieve_file, args=("retrThread",c)) t.start() s.close() if __name__ == '__main__': main()client
#CLIENT import socket def main(): host = "127.0.0.1" port = 5000 s=socket.socket() s.connect((host,port)) filename = input("enter file name ->") if filename != 'q': s.send(filename.encode()) data = s.recv(1024) data = data.decode() if data.startswith('EXISTS'): filesize = int(data[6:]) message= input("this file exists " + str(filesize) + "bytes, download(Y/N)?") if message == 'Y': s.send(b'OK') f=open('./received/' + filename, 'wb') data=s.recv(1024) total_received=len(data) f.write(data) while total_received < filesize: data=s.recv(1024) total_received += len(data) f.write(data) print("download complete") else: print("file doesn't exist, got:", repr(data)) s.close() if __name__ == '__main__': main()both files are in same folder and I created
received
subfolder in advance
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs