服务器在请求header后只返回了一部分内容



好的,我有这个奇怪的问题,如果我问内容后做一个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.1rnHost: example.comrnUser-Agent: py-botrnrn')
# 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("rn")
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.1rnHost: example.comrnUser-Agent: py-botrnrn")
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>

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

相关内容

最新更新