Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file doesn't exist
#11
now the file is downloaded, but I cannot see it anywhere.
Shouldn't I see a duplicate of the file I have on the same directory as the python file?
Since 'upper.txt' is a file that I am taking from this directory, sending it to the client, and downloading it from the client console.
Reply
#12
Everywhere in the code you compare bytestrings with strings. So the file is never send
        if user_response[:2] == 'OK':
this code from SERVER will never be True

You need to fix this and elsewhere (if any) in the code where you make such tests (data received via socket)... the same way like the other case - either decode bytestring or compare with bytestring, nit string
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
#13
I added this line
send_bytes=send_bytes.decode()
after
   send_bytes=f.read(1024)
in the server file, but I still have the same problem
Reply
#14
really? Sorry, but are you not able to follow directions?
(Oct-24-2019, 09:24 AM)buran Wrote: Everywhere in the code you compare bytestrings with strings. So the file is never send
if user_response[:2] == 'OK':
this code from SERVER will never be True

You need to fix this and elsewhere (if any) in the code where you make such tests (data received via socket)... the same way like the other case - either decode bytestring or compare with bytestring, nit string

what effect would your change have to fix above problem?
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
#15
I am sorry, but I am having an hard time understanding you.
All I am understanding is that I have to decode all the bytestring,
that's why I am using the command .decode()
just like it was done for the client.
But I am not sure of what else I need to do.
Now I added this line
user_response=user_response.decode()
right before the comparison, but still, it doesn't work.

I replaced those lines with
user_response = sock.recv(1024)
        user_response=user_response.decode()
        if user_response.startswith('OK'):
            with open(filename, 'rb') as f:
but why doesn't it still work ?
Reply
#16
did you also revert this change?
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
#17
Yes, that line is no longer there, but the file is still not sent
Reply
#18
the only other thing I see
is to change in CLIENT this:
s.send('OK')
to
s.send(b'OK')
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
#19
Still not working.
I am here sending again my edited codes
in case:

#SERVER
import socket
import threading
import os

os.chdir(os.path.abspath(os.path.dirname(__file__)))

from idlelib.iomenu import encoding


def retrieve_file(name, sock):
    filename=sock.recv(1024)
    if os.path.isfile(filename):
        sock.send( ("EXISTS" + str(os.path.getsize(filename))).encode(encoding) )
        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
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(filesize, '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()
Reply
#20
Oh, in Client
change
f=open(filesize, 'wb')
to
f=open(filename, 'wb')
your current code should raise OSError: [Errno 9] Bad file descriptor, why don't you say so?

after that change it worked for me
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,251 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