Python 服务器在请求标头后只返回一部分内容

Python 服务器在请求标头后只返回一部分内容,python,http,http-headers,Python,Http,Http Headers,好吧,我有一个奇怪的问题,如果我在做了一个HEAD请求之后问内容,主持人会给出一点内容。如果我在询问内容之前没有执行HEAD请求,我将正确地收到页面。经过一些测试,我发现如果我再次要求提供内容,它会提供缺少的其余内容。目标页面是。代码如下: #it isn't printing the whole website now. why. import socket import sys usr_choice = str(input("Do you choose to only down

好吧,我有一个奇怪的问题,如果我在做了一个HEAD请求之后问内容,主持人会给出一点内容。如果我在询问内容之前没有执行HEAD请求,我将正确地收到页面。经过一些测试,我发现如果我再次要求提供内容,它会提供缺少的其余内容。目标页面是。代码如下:

#it isn't printing the whole website now. why.

import socket
import sys

usr_choice = str(input("Do you choose to only download the header (1) or the header and body? (2)"))

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connecting with the server (example.com)
s.connect(('example.com', 80))
s.settimeout(10)

s.send(b'HEAD /index.html HTTP/1.1\r\nHost: example.com\r\nUser-Agent: py-bot\r\n\r\n')

# The "response_header" will first be the data we receive, then it will become itself decoded then it will be itself parsed.

response_header = s.recv(512)
response_header = response_header.decode()
if usr_choice == "2":
    response_header = response_header.split("\r\n")
print(response_header)

# Reviewing the HTTP Header "Content-Length"
if usr_choice == "2":
    response_size = 0
    print(response_size)
    for i in response_header:
        if "Content-Length" in i:
            response_size+=int(i.replace("Content-Length: ",""))

    s.send(b"GET /index.html HTTP/1.1\r\nHost: example.com\r\nUser-Agent: py-bot\r\n\r\n")

    if response_size == 0:
        print("Header incomplete.")
        sys.exit(1)
    print(response_size)
    full_response = s.recv(8192)

    print(full_response.decode())
下面是它输出的内容:

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior co

示例域
身体{
背景色:#F0F2;
保证金:0;
填充:0;
字体系列:-苹果系统、系统用户界面、BlinkMacSystemFont、“Segoe用户界面”、“开放式SAN”、“Helvetica Neue”、Helvetica、Arial、无衬线字体;
}
div{
宽度:600px;
保证金:5em自动;
填料:2米;
背景色:#fdff;
边界半径:0.5em;
盒影:2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:链接,a:已访问{
颜色:#38488f;
文字装饰:无;
}
@介质(最大宽度:700px){
div{
保证金:0自动;
宽度:自动;
}
}
示例域
此域用于文档中的示例。你可以用这个
文献中没有事先合作的领域
如果我再次询问主机内容,它将返回页面的其余部分:

ordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
排序或请求许可


回答我自己的问题:TCP将数据分块,这样就不会损坏数据或类似的东西