非常基本的Python web服务器-奇怪的问题

非常基本的Python web服务器-奇怪的问题,python,webserver,Python,Webserver,我刚刚开始使用Python,并尝试编写一个简单的web服务器。除了我有一个小问题外,一切似乎都正常。当我从web服务器请求一个特定的文件(如Test.html)时,html文件中的数据在我的客户机中反复出现,就像它卡在一个循环中一样。因此,我看到的不是web客户端中只显示一次“Test”,而是“Test…多次”。这可能是一个简单的错误,但我希望有人能给我指出正确的方向 谢谢你的帮助 import socket import sys serversocket = socket.socket(so

我刚刚开始使用Python,并尝试编写一个简单的web服务器。除了我有一个小问题外,一切似乎都正常。当我从web服务器请求一个特定的文件(如Test.html)时,html文件中的数据在我的客户机中反复出现,就像它卡在一个循环中一样。因此,我看到的不是web客户端中只显示一次“Test”,而是“Test…多次”。这可能是一个简单的错误,但我希望有人能给我指出正确的方向

谢谢你的帮助

import socket
import sys

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket Created!!")

try:
#bind the socket
    #fill in start
        server_address = ('localhost', 6789)
        serversocket.bind(server_address)
    #fill in end
except socket.error as msg:
    print("Bind failed. Error Code: " + str(msg[0]) + "Message: " + 
msg[1])
    sys.exit()  
print("Socket bind complete")

#start listening on the socket
#fill in start
serversocket.listen(1)
#fill in end
print('Socket now listening')

while True: 
#Establish the connection 
    connectionSocket, addr = serversocket.accept()
    print('source address:' + str(addr))
    try:
    #Receive message from the socket
        message = connectionSocket.recv(1024)  
        print('message = ' + str(message))
        #obtian the file name carried by the HTTP request message
        filename = message.split()[1] 
        print('filename = ' + str(filename))
        f = open(filename[1:], 'rb') 
        outputdata = f.read()
        #Send the HTTP response header line to the socket
        #fill in start
        connectionSocket.send(bytes('HTTP/1.1 200 OK\r\n\r\n','UTF-
8'))
        #fill in end
        #Send the content of the requested file to the client
        for i in range(0, len(outputdata)):  
            connectionSocket.send(outputdata)

        #close the connectionSocket
        #fill in start
        connectionSocket.close()
        #fill in end

        print("Connection closed!")
    except IOError: 
        #Send response message for file not found 
        connectionSocket.send(bytes("HTTP/1.1 404 Not 
Found\r\n\r\n","UTF-8"))
        connectionSocket.send(bytes("<html><head></head><body><h1>404 
Not Found</h1></body></html>\r\n","UTF-8"))

#Close the client socket 
        #fill in start
        connectionSocket.close()
serverSocket.close()
导入套接字
导入系统
serversocket=socket.socket(socket.AF\u INET,socket.SOCK\u流)
打印(“已创建套接字!!”)
尝试:
#捆绑插座
#填写开始
服务器地址=('localhost',6789)
serversocket.bind(服务器地址)
#填完
除了socket.error作为消息:
打印(“绑定失败。错误代码:“+str(msg[0])+”消息:”+
味精[1])
sys.exit()
打印(“套接字绑定完成”)
#开始监听套接字
#填写开始
serversocket.listen(1)
#填完
打印('套接字正在侦听')
尽管如此:
#建立连接
connectionSocket,addr=serversocket.accept()
打印('源地址:'+str(地址))
尝试:
#从套接字接收消息
message=connectionSocket.recv(1024)
打印('message='+str(message))
#obtian HTTP请求消息携带的文件名
filename=message.split()[1]
打印('filename='+str(filename))
f=打开(文件名[1:],'rb')
outputdata=f.read()
#将HTTP响应头行发送到套接字
#填写开始
connectionSocket.send(字节('HTTP/1.1 200 OK\r\n\r\n','UTF-
8'))
#填完
#将请求文件的内容发送到客户端
对于范围(0,len(outputdata))中的i:
connectionSocket.send(输出数据)
#关闭连接插座
#填写开始
connectionSocket.close()连接
#填完
打印(“连接已关闭!”)
除IOError外:
#未找到文件的发送响应消息
connectionSocket.send(字节)(“HTTP/1.1 404不是
已找到\r\n\r\n“UTF-8”))
connectionSocket.send(字节(“404
找不到\r\n(“UTF-8”))
#关闭客户端套接字
#填写开始
connectionSocket.close()连接
serverSocket.close()
你陷入了一个循环:)

您正在发送
outputdata的内容
文件长度的次数

您只需要
connectionSocket.send(outputdata)
而不需要for..循环就可以发送一次

此外,请确保关闭从中读取内容的文件。(
f.close()

你陷入了一个循环:)

您正在发送
outputdata的内容
文件长度的次数

您只需要
connectionSocket.send(outputdata)
而不需要for..循环就可以发送一次


此外,请确保关闭从中读取内容的文件。(
f.close()

为什么要这样做?只是导入烧瓶为什么要这样做?只需导入烧瓶
    #fill in end
    #Send the content of the requested file to the client
    for i in range(0, len(outputdata)):  
        connectionSocket.send(outputdata)