简单Python TCP服务器不发送整个网页

简单Python TCP服务器不发送整个网页,python,html,tcp,Python,Html,Tcp,我是一名初级COMSCI学生,我正在尝试用python编写一个简单的服务器,它接收存储在同一目录中的.HTML页面,并使用TCP连接将其发送到同一网络上的客户端 这是我的代码: from socket import * serverPort = 8000 serverSocket = socket(AF_INET, SOCK_STREAM) # Prepare a sever socket serverSocket.bind(('', serverPort)) # binds socket

我是一名初级COMSCI学生,我正在尝试用python编写一个简单的服务器,它接收存储在同一目录中的.HTML页面,并使用TCP连接将其发送到同一网络上的客户端

这是我的代码:

from socket import *

serverPort = 8000
serverSocket = socket(AF_INET, SOCK_STREAM)
# Prepare a sever socket
serverSocket.bind(('', serverPort))  # binds socket to port 8000
serverSocket.listen(1)  # waiting for client to initiate connection

while True:
    # Establish the connection
    print('Ready to serve...')
    connectionSocket, addr = serverSocket.accept()
    try:

        message = connectionSocket.recv(1024)
        filename = message.split()[1]

        f = open(filename[1:].decode())
        outputdata = f.read()

        # Send one HTTP header line into socket
        http_response = 'HTTP/1.1 200 OK\n'

        connectionSocket.send(http_response.encode())

        # 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()
    except IOError:
        connectionSocket.send("\r\n".encode())
        # DO LATER
serverSocket.close()
sys.exit()
这是我的简单html页面:

<!DOCTYPE html>
<html>
  <body>
    <h1>My First Web Page</h1>

    <p>You have successfully accessed the Web Server</p>
  </body>
</html>

我的第一个网页
您已成功访问Web服务器

到目前为止,每当我运行服务器并将浏览器指向它时,我只能得到以下服务:

<p>You have successfully accessed the Web Server</p>
您已成功访问Web服务器

以及之后的body和html标记。检查页面源没有标题

我在尝试访问我的服务器时运行了Wireshark,事实上,我似乎只通过“You have successfully access the Web server”(您已成功访问了Web服务器)及以后的方式发送信息。尽管打印函数显示我确实通过TCP连接发送了文件中的所有数据,但这是事实


有人知道问题出在哪里吗?

我会使用
http.server

导入http.server
导入socketserver
端口=8080
Handler=http.server.SimpleHTTPRequestHandler
将socketserver.TCPServer((“”,PORT),Handler)作为httpd:
httpd.永远为你服务()

来源:

发送协议应答和头后,实际响应在两个
\r\n
序列之后

使用此固定代码:

from socket import *

serverPort = 8000
serverSocket = socket(AF_INET, SOCK_STREAM)
# Prepare a sever socket
serverSocket.bind(('', serverPort))  # binds socket to port 8000
serverSocket.listen(1)  # waiting for client to initiate connection

while True:
    # Establish the connection
    print('Ready to serve...')
    connectionSocket, addr = serverSocket.accept()
    try:

        message = connectionSocket.recv(1024)
        filename = message.split()[1]

        f = open(filename[1:].decode())
        outputdata = f.read()

        # Send one HTTP header line into socket
        http_response = 'HTTP/1.1 200 OK\n'

        connectionSocket.send(http_response.encode())
        connectionSocket.send("\r\n".encode())
        connectionSocket.send("\r\n".encode())

        # Send the content of the requested file to the client
        for i in range(0, len(outputdata)):
            connectionSocket.send(outputdata[i].encode())

        connectionSocket.close()
    except IOError:
        # DO LATER
serverSocket.close()
sys.exit()

是否只允许使用套接字库?否则,您可以使用http之类的库。server@tschomacker我认为我们可以导入其他人,但我觉得他们不会希望我们做一些我们没有被介绍过的事情?我不知道我是否可以,因为他们肯定希望我们使用这行代码:“对于范围(0,len(outputdata)):/connectionSocket.send(outputdata[i].encode())”非常感谢。成功了。我可以问一下,为什么在发送数据之后也要删除\r\n吗?@reetyn据我所知,响应的结尾没有分隔符。你可以离开它,但它将是网站HTML的一部分。@ ReTyn如果这个答案对你有帮助,你可以考虑接受答案,这样它也会帮助其他人。