Oct-23-2019, 09:09 PM
I am trying to display an image through socket programming,
I have a server and a client file, and they seem to run fine,
however, once I write my file in the console (see input command),
I always obtain the message 'file doesn't exist'.
I am not sure why, because the files I use are in the same directory as
the python files.
Any ideas?
I have a server and a client file, and they seem to run fine,
however, once I write my file in the console (see input command),
I always obtain the message 'file doesn't exist'.
I am not sure why, because the files I use are in the same directory as
the python files.
Any ideas?
#SERVER import socket import threading import os 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) if user_response[:2] == '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) if data[:6] == 'EXISTS': filesize = int(data[:6]) message= input("this file exists " + str(filesize) + "bytes, download(Y/N)?") if message == 'Y': s.send('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") s.close() if __name__ == '__main__': Main()