我已成功地從瀏覽器訪問我的網絡服務器,在服務器上下載了一個文件,並使用chrome正確查看了它。但是,當服務器預留約。 20秒,它會崩潰與IndexError。Python web服務器 - 分割文件名時出現IndexError
from socket import *
serverport = 972
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverport))
serverSocket.listen(1)
print 'Standing by...'
while True:
#Establish the connection
connectionSocket, addr = serverSocket.accept()
try:
message = connectionSocket.recv(1024)
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.read()
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i])
print 'Success! File sent!'
connectionSocket.close()
except IOError:
errormessage = 'Error 404 - File not found'
connectionSocket.send(errormessage)
輸出我得到的是以下幾點:
Standing by..
Success! File sent! #sent everytime i request the webpage on the client localhost:80/helloworld.html
Traceback (most recent call last):
File "C:/Users/Nikolai/Dropbox/NTNU/KTN/WebServer/TCPServer.py", line 14, in <module>
filename = message.split()[1]
IndexError: list index out of range
嘗試'message = connectionSocket.recv(1024)[2:]' –
當你添加它時,你的'logging.debug(message)'語句在錯誤行之前輸出了什麼? – geoffspear
我不知道爲什麼有人低調。他在SO的第一個問題,這是很好的陳述。 –