# 阻塞 I/O ->收快递,快递如果不到,就干不了其他的活
import socket
import time
def blocking(pn): #页码
sock = socket.socket()
url = 'www.baidu.com'
port = 80
sock.connect((url,port)) #请求百度
request = 'GET {} HTTP/1.0\r\nHost: www.baidu.com\r\n\r\n'.format('/s?wd={}'.format(pn))
sock.send(request.encode()) #编码发送 rrequest的内容
response = b''
inf = sock.recv(1024)
while inf: #因为一次接收不完, 所以循环接收
response += inf
inf = sock.recv(1024)
#print(response.decode()) #解码打印
return response
#blocking(1)
def block_way():
for i in range(10):
blocking(i) #搜索wd = 1 和wd = 2 和wd = 3 和wd = 4 和 wd = 5
if __name__ == '__main__':
start_time = time.time()
block_way()
print("请求10次耗时为:{}".format(time.time() - start_time)) #字符串拼接