networking - Send file from Client to Server in python -
i sending file client server in python. i've wrote python script. i want store file @ receiver side same filename sent sender. suppose sending file "first.txt". @ receiver side saving file name file_1.txt. instead of file_1.txt, want original name first.txt. 1 more thing, if i'm sending file after first.txt, overwriting previous file @ receiver. i'm not getting idea how solve problem.please me.
server code (receiver)
import socket import shutil s = socket.socket(socket.af_inet, socket.sock_stream) host = '' port = 31401 s.bind((host, port)) s.listen(3) conn, addr = s.accept() print 'conn @ address',addr conn.sendall('ready') i=1 f = open(r'file_'+ str(i)+".txt",'wb') i=i+1 fsize=int(conn.recv(8)) print 'file size',fsize sfile = conn.makefile("rb") shutil.copyfileobj(sfile, f) sfile.close() f.write(conn.recv(fsize)) f.close() conn.close() s.close()
client code (sender)
import socket, os, shutil stat import st_size host = raw_input("please enter ip-address : ") port = 31400 s = socket.socket(socket.af_inet, socket.sock_stream) s.connect((host,port)) if s.recv(8)!='ready': raw_input('unable connect \n\n press key exit ...') s.close() exit() path=raw_input("enter path of file : ") f=open(path,'rb') fsize=os.stat(f.name)[st_size] s.sendall(str(fsize).zfill(8)) sfile = s.makefile("wb") shutil.copyfileobj(f, sfile) sfile.close() s.close() f.close()
Comments
Post a Comment