简体   繁体   中英

Sending file from server to client (python)

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) ?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM