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.
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
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
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?
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 ?
Yes, that line is no longer there, but the file is still not sent
the only other thing I see
is to change in CLIENT this:
s.send('OK')
to
s.send(b'OK')
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()
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