繁体   English   中英

将文件从服务器发送到客户端(Python)

[英]Sending file from server to client (python)

我当前正在编写一个服务器客户端应用程序,该应用程序需要传输一些文件才能正常工作。 我正在使用这种方法:

客户:

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"

服务器:

def receive_file(self, name, arg):                                        
    with open("./sampletest/"+name, "wb") as handle: 
        handle.write(arg.data)

但是,我该怎么做(我的意思是从服务器向客户端发送文件)呢?

只需在服务器上这样编写一个函数:

def send_file(self, name):
  with open('./sampletest/' + name, 'rb') as handle:
    return handle.read()

并在客户端上调用:

data = send_file(fileName)
with open('./received_files/' + fileName, 'wb') as handle:
  handle.write(data)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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