簡體   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