问题
I'm currently writing a server-client application that need to transfer some file to work. I'm using this method:
client:
file_to_send = raw_input(">")
try:
f = open("./sent_files/" + file_to_send, "rb")
except IOError, e:
print ">error: ", e
break
data = xmlrpclib.Binary(f.read())
if s.receive_file(file_to_send, data):
print ">file correctly sent"
server:
def receive_file(self, name, arg):
with open("./sampletest/"+name, "wb") as handle:
handle.write(arg.data)
But how can I do the opposite (I mean sending a file from the server to the client) ?
回答1:
Just write a function on the server like this:
def send_file(self, name):
with open('./sampletest/' + name, 'rb') as handle:
return handle.read()
and call this on the client:
data = send_file(fileName)
with open('./received_files/' + fileName, 'wb') as handle:
handle.write(data)
来源:https://stackoverflow.com/questions/16790725/sending-file-from-server-to-client-python