我正在努力完成这个实验室,但似乎无法使它正常工作。当我试图获取服务器不存在的文件时,我
This page isn’t working 127.0.0.1 sent an invalid response. ERR_INVALID_HTTP_RESPONSE
我想得到的答复是
404 File not found
当我试图加载一个不存在的文件时,编译器对第16行说:
filename = message.split()[1] IndexError: list index out of range
代码编译,我可以打开我的helloworld文件,但我就是不能得到这个404错误。我得到了一个基本的代码,所以有一些东西,我不能改变不偏离课程材料。你知道吗
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a server socket
serverPort = 7000
serverSocket.bind(('127.0.0.1', serverPort))
serverSocket.listen(5)
while True:
print('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
#Fill in start #Fill in end
try:
message = connectionSocket.recv(1024)
print (message)
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.read()
#Send one HTTP header line into socket
#Fill in start
connectionSocket.send('\nHTTP/1.x 200 OK\n'.encode())
#Fill in end
#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.send("\r\n".encode())
connectionSocket.close()
print ('File Recieved')
except IOError:
connectionSocket.send('\n404 File Not Found\n'.encode())
connectionSocket.close()
#Close client socket
serverSocket.close()
sys.exit()
基本代码似乎是python2,我使用的是python3。我做了一些小的语法调整。你知道吗
删除print(message)在编译器中生成“File received”,但在浏览器中仍然没有404错误。8小时后我不知所措。你知道吗
处理
message.split()[1]
中的IndexError
的一种方法是处理message.split()[1]
中的IndexError
;)相关问题 更多 >
编程相关推荐