我正在尝试构建一个简单的多线程服务器,当通过浏览器查询时,会回复所请求的html页面或404页面。
这是我的代码:
from socket import *
import thread
def handler(connectionSocket,addr):
try:
message = connectionSocket.recv(2048)
filename = message.split()[1]
f = open(filename[1:], 'r')
outputdata = f.read()
#Send one HTTP header line into socket
connectionSocket.send("\nHTTP/1.0 200 OK\n\n")
#Send the content of the requested file to the client
connectionSocket.send(outputdata)
#Close client socket
f.close()
connectionSocket.close()
except IOError:
#Send response message for file not found
print "404 Not Found"
#Send over 404 page
f = open("404.php",'r')
outputdata = f.read()
connectionSocket.send("\nHTTP/1.0 404 Not Found\n\n")
connectionSocket.send(outputdata)
#Close client socket
f.close()
connectionSocket.close()
def main():
#Prepare a sever socket
serverPort = 6789
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print "Starting server on port: " + str(serverPort) + ". User <CTRL-C> to stop"
while True:
#Establish the connection
print 'Ready to serve...'
connectionSocket, addr = serverSocket.accept()
print '...connected from:', addr
thread.start_new_thread(handler,(connectionSocket,addr))
serverSocket.close()
if __name__ == '__main__':
main()
当我查询helloworld.html时,我收到了网页,但服务器似乎连接了4次,如下所示。页面显示后,它会一直保持“准备服务......”#34;几分钟然后得到错误。
Starting server on port: 6789. User <CTRL-C> to stop
Ready to serve...
...connected from: ('127.0.0.1', 61382)
Ready to serve...
...connected from: ('127.0.0.1', 61386)
Ready to serve...
...connected from: ('127.0.0.1', 61387)
Ready to serve...
...connected from: ('127.0.0.1', 61388)
Ready to serve...
Unhandled exception in thread started by <function handler at 0x10051a410>
Traceback (most recent call last):
File "web_server2.py", line 13, in handler
filename = message.split()[1]
IndexError: list index out of rangeUnhandled exception in thread started by <function handler at 0x10051a410>
Traceback (most recent call last):
File "web_server2.py", line 13, in handler
filename = message.split()[1]
IndexError: list index out of range
Unhandled exception in thread started by <function handler at 0x10051a410>
Traceback (most recent call last):
File "web_server2.py", line 13, in handler
filename = message.split()[1]
IndexError: list index out of range
^CTraceback (most recent call last):
File "web_server2.py", line 58, in <module>
main()
File "web_server2.py", line 51, in main
connectionSocket, addr = serverSocket.accept()
File "//anaconda/lib/python2.7/socket.py", line 206, in accept
sock, addr = self._sock.accept()
从错误回溯中,似乎程序连接并发送页面,然后认为它再次连接,但是当没有发送查询时,数组为空并且以一个边界错误退出。
同样,当我查询不在那里的页面并收到我的404页面时:
Starting server on port: 6789. User <CTRL-C> to stop
Ready to serve...
...connected from: ('127.0.0.1', 55032)
Ready to serve...
...connected from: ('127.0.0.1', 55033)
Ready to serve...
404 Not Found
...connected from: ('127.0.0.1', 55059)
Ready to serve...
...connected from: ('127.0.0.1', 55061)
Ready to serve...
...connected from: ('127.0.0.1', 55063)
Ready to serve...
...connected from: ('127.0.0.1', 55065)
Ready to serve...
...connected from: ('127.0.0.1', 55067)
Ready to serve...
...connected from: ('127.0.0.1', 55069)
Ready to serve...
...connected from: ('127.0.0.1', 55071)
Ready to serve...
...connected from: ('127.0.0.1', 55073)
Ready to serve...
...connected from: ('127.0.0.1', 55075)
Ready to serve...
...connected from: ('127.0.0.1', 55077)
Ready to serve...
...connected from: ('127.0.0.1', 55079)
Ready to serve...
...connected from: ('127.0.0.1', 55093)
Ready to serve...
404 Not Found
...connected from: ('127.0.0.1', 55095)
Ready to serve...
404 Not Found
它连接多次,但只通过ioexception部分3次,因为只有3次&#34; 404 Not Found&#34;的打印。但是现在,服务器没有像helloworld.html页面那样退出,它完全保持原样,当再次查询不存在的页面时,我得到另一个404.但是当查询helloworld.html时,它显示它然后退出与上面相同的错误。
任何寻找bug的帮助都会很棒。我可以通过测试来判断它似乎与我处理i / o的方式有关,但我不确定这似乎是什么问题。这是我的第一个服务器程序,并希望了解更多相关信息。谢谢!