服务器在请求 header

时间:2021-01-22 06:15:19

标签: python http http-headers

好的,我遇到了这个奇怪的问题,如果我在执行 HEAD 请求后询问内容,主机会提供一些内容。如果我在询问内容之前不执行 HEAD 请求,我将正确接收页面。经过一些测试,我发现如果我再次询问内容,它会提供缺少的其余内容。目标页面是example.com。代码如下:

#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

如果我再次向主持人询问内容,它将返回页面的其余部分:

ordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

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