send and receive a file in python sockets -
this has part been answered here have been trying modify server (to send) , client (to receive) feedback server upon receipt of entire file.
below client server code; presently both client , server hang. ie. indefintely go "busy" state (both client , server nothing, until ctrl-c on server reports failure here "l = sc.recv(1024)" , ctrl-c on client reports failure here "reply =s.recv(1024)")
import socket import sys s = socket.socket() s.bind(("localhost",3000)) s.listen(10) i=1 while true: sc, address = s.accept() print address f = open('tranmit.jpg",'wb') #open in binary l = 1 while(l): l = sc.recv(1024) while (l): f.write(l) l = sc.recv(1024) f.close() sc.send("received") # send response sc.close() s.close() ------------------------------------------------------------ #client s = socket.socket() s.connect(("localhost",3000)) f=open ("tranmit.jpg", "rb") l = f.read(1024) while (l): s.send(l) l = f.read(1024) reply =s.recv(1024) # feedback: receive feedback server. print reply s.close()
the code works fine without feedback lines added in (as in link above), have idea on how keep working feedback well? thanks.
your client must indicate, somehow, entire file has been sent. stands, server has no way of knowing when client's send operations complete.
one solution call socket.shutdown
after transmissions complete. here new version of client program. no changers required server program.
#client import socket s = socket.socket() s.connect(("localhost",3000)) f=open ("tranmit.jpg", "rb") l = f.read(1024) while (l): s.send(l) l = f.read(1024) s.shutdown(socket.shut_wr) reply =s.recv(1024) # feedback: receive feedback server. print reply s.close()
Comments
Post a Comment