2013-05-28 160 views
0

我目前正在編寫一個服務器端客戶端應用程序,需要傳輸一些文件才能工作。 我使用這種方法:發送文件從服務器到客戶端(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) 

可是我該怎麼做相反的(我的意思是從服務器發送一個文件給客戶)?

+0

看來,客戶端和服務器在同一個運行機器,並且您只需在客戶端代碼中調用服務器的功能。 – Roger

回答

5

只寫一個函數服務器上是這樣的:

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) 
+0

非常感謝,它工作:) – ste

+0

歡迎來到Stackoverflow!如果答案正在解決您的問題,請接受它(點擊左邊的複選標記)。獨立於此,如果答案似乎對您有幫助,請將其提高一級(向左移動向上箭頭)。這將提高回答者的聲譽。 – Alfe

相關問題