problems encountered when starting to learn socket (python),
Liao Xuefeng"s socket learning website:
https://www.liaoxuefeng.com/w.
problem: when the while cycle accepts the returned content from Baidu, the last acceptance will stay for a long time, and the normal access request is certainly not so slow. I don"t know what the problem is.
attach your own code:
-sharp socket:
import socket
-sharp socket:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-sharp :
s.connect(("www.baidu.com", 80))
-sharp :
s.send(b"GET / HTTP/1.1\r\nHost:www.baidu.com\r\n\r\n")
-sharp :
buffer = []
while True:
-sharp 1k:
d = s.recv(1024)
-sharp1024d==""
print(d)
if d:
buffer.append(d)
else:
break
data = b"".join(buffer)
s.close()
header, html = data.split(b"\r\n\r\n", 1)
print(header.decode("utf-8"))
-sharp :
with open("sina.html", "wb") as f:
f.write(html)
stop for several seconds (red box)