Sending file from server to client (python)

回眸只為那壹抹淺笑 提交于 2019-12-12 01:05:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!