Oct-24-2019, 10:42 AM
Oct-24-2019, 10:46 AM
(Oct-24-2019, 10:42 AM)mcgrim Wrote: [ -> ]I am not sure how to implement that.
How do you do it ?
There is nothing to implement. Your current code produce an error. You don't says so...
In your current code you use number - integer (the
filesize
) as file name. you want to use the filename
insteadI show which line to replace with what to replace
Also - make sure that both scripts are in different folders or make sure to save the received file in different location, e.g. subfolder
Oct-24-2019, 10:51 AM
so you are saying that just by replacing line 22
Also, I have the client, server, and file in the same folder.
Should I have client and server separated and the new file into another folder?
f=open(filesize, 'wb')with
f=open(filename, 'wb')it should work, right?
Also, I have the client, server, and file in the same folder.
Should I have client and server separated and the new file into another folder?
Oct-24-2019, 10:57 AM
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 advanceOct-24-2019, 11:11 AM
unfortunately the new folder is still empty,
does the new folder directory matter ?
does the new folder directory matter ?
Oct-24-2019, 11:14 AM
the scripts I posted above WORK for me - I TESTED it
the new folder matter if the server and client are in same fodler. you open a file to read from it (in the server) and you would try to open it for writing (in the client). Obviously if both scripts are in the same folder that will be not possible
server is running. then
the new folder matter if the server and client are in same fodler. you open a file to read from it (in the server) and you would try to open it for writing (in the client). Obviously if both scripts are in the same folder that will be not possible
server is running. then
Output:$ ls ./received
$ python3 client.py
enter file name ->values.json
this file exists 45bytes, download(Y/N)?Y
download complete
$ ls ./received
values.json
as you can see from ls
command ./received
is empty and then the file (some json file I have) is downloadedOct-24-2019, 11:20 AM
you misunderstood my question:
does the new folder 'received' need to be created somewhere specific
or its location doesn't matter ?
does the new folder 'received' need to be created somewhere specific
or its location doesn't matter ?
Oct-24-2019, 11:23 AM
If the both scripts are in the same folder and this folder is the current working directory from which you run the scripts
because you have this line
of course you can specify absolute path where to save the received file in which case it will doesn't matter
received
should be subfolder.because you have this line
os.chdir(os.path.abspath(os.path.dirname(__file__)))
which I removed in my scripts, I guess your current working directory is the folder in which your files areof course you can specify absolute path where to save the received file in which case it will doesn't matter
Oct-24-2019, 11:29 AM
simply put:
the "client-server" folder contains the 'client' file, the 'server' file
and the 'received' folder.
Is it correct ?
the "client-server" folder contains the 'client' file, the 'server' file
and the 'received' folder.
Is it correct ?
Oct-24-2019, 11:30 AM
yes