2013-05-10 11 views
-1

我在學習http協議的同時在python中創建一個http服務器。我遇到了一個問題。我想通過多個數據包將內容發送到瀏覽器。我試過這個:python http服務器 - 內容在多個數據包中

conn.send("HTTP/1.0 200 OK") 
conn.send("\r\n") 
conn.send("Content-Type: application/force-download") 
conn.send("\r\n") 
conn.send("Content-Disposition: attachment; filename=file.txt") 
conn.send("\r\n") 
conn.send("Content-length: "+str(file_length)) 
conn.send("\r\n\r\n") 

c = 0 
while download_data[c]: 
    conn.send(download_data[c]) 

但它不起作用。瀏覽器只接收第一個數據包。感謝您的建議。

+0

你的''conn''和''download_data''變量的類型是什麼? – 2013-05-10 13:06:15

+0

conn是套接字對象,download_data包含部分內容。 – 2013-05-10 13:16:50

回答

0

我沒有看到c更改的地方,所以download_data[c]的值也不會改變。你需要:

c = 0 
while c < len(download_data): 
    conn.send(download_data[c]) 
    c += 1 
+0

謝謝,我匆忙編寫了這部分代碼。我需要編輯它發送的所有頭文件。 – 2013-05-10 13:15:48