Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file doesn't exist
#21
I am not sure how to implement that.
How do you do it ?

by just changing filesize to filename, it still doesn't work
Reply
#22
(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 instead
I 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
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

Reply
#23
so you are saying that just by replacing line 22
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?
Reply
#24
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. received and
f=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

Reply
#25
unfortunately the new folder is still empty,
does the new folder directory matter ?
Reply
#26
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
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 downloaded
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

Reply
#27
you misunderstood my question:

does the new folder 'received' need to be created somewhere specific
or its location doesn't matter ?
Reply
#28
If the both scripts are in the same folder and this folder is the current working directory from which you run the scripts 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 are
of course you can specify absolute path where to save the received file in which case it will doesn't matter
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

Reply
#29
simply put:
the "client-server" folder contains the 'client' file, the 'server' file
and the 'received' folder.

Is it correct ?
Reply
#30
yes
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

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML file uploaded through python screen doesn't look as expected miker2808 6 5,236 Aug-04-2018, 02:05 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020