Python WebServer打破了.send消息?

时间:2014-10-12 23:58:17

标签: python html sockets python-2.7

插入.send以发送OK消息显然会使其余代码无法正常工作? 如果我从以下代码中删除client.send消息,则可以正常工作。但有了它,浏览器中没有任何反应,在Firefox中检查,它说请求已经完成,但是没有显示任何页面......它只是空白。
为什么.send消息不会导致任何事情发生?

from socket import *
server = socket(AF_INET, SOCK_STREAM)
port = 12030
server.bind((gethostname(), port))
server.listen(1)
while True:
    print 'Ready to serve'
    conection,  addr = server.accept()
    try:
        print 'Working'
        message = conection.recv(1024)
        conection.send("HTTP/1.0 200 OK\r\n")
        conection.send("Content-Type:text/html\r\n")
        filename = message.split()[1] 
        print "FILENAME", filename
        f = open(filename[1:]) #cuts off the '/' in the request page
        outputdata = f.read()
        print "OUTDATA: ", outputdata
        for i in range(0, len(outputdata)):
            conection.send(outputdata[i])
        conection.close()
    except IOError:
        print 'IO ERROR'
        conection.send("404 NOT FOUND")
        print message
        conection.close()
    except KeyboardInterrupt:
        server.close()
        conection.close()
        break;

如此处所示,它根本不会影响数据流。

user @@$$ python webServer.py 
Ready to serve
Working
FILENAME /HelloWorld.html
OUTDATA:  <html>Hello World</html>

Ready to serve

0 个答案:

没有答案