Posts: 203
Threads: 41
Joined: Mar 2019
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.
Posts: 8,165
Threads: 160
Joined: Sep 2016
Oct-24-2019, 09:24 AM
(This post was last modified: Oct-24-2019, 09:24 AM by buran.)
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
Posts: 203
Threads: 41
Joined: Mar 2019
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
Posts: 8,165
Threads: 160
Joined: Sep 2016
Oct-24-2019, 09:31 AM
(This post was last modified: Oct-24-2019, 09:31 AM by buran.)
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?
Posts: 203
Threads: 41
Joined: Mar 2019
Oct-24-2019, 09:36 AM
(This post was last modified: Oct-24-2019, 09:53 AM by mcgrim.)
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 ?
Posts: 8,165
Threads: 160
Joined: Sep 2016
did you also revert this change?
Posts: 203
Threads: 41
Joined: Mar 2019
Yes, that line is no longer there, but the file is still not sent
Posts: 8,165
Threads: 160
Joined: Sep 2016
the only other thing I see
is to change in CLIENT this:
s.send('OK') to
s.send(b'OK')
Posts: 203
Threads: 41
Joined: Mar 2019
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()
Posts: 8,165
Threads: 160
Joined: Sep 2016
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
|