socket_http
import socket from urllib.parse import urlparse import time def get_url(url): # 通过socket请求html url = urlparse(url) # 解析url host = url.netloc # 获取ip地址 path = url.path # 相对主路径下的请求路径 if path == "": path = "/" # 建立socket连接 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建socket连接 # client.setblocking(False) client.connect((host, 80)) # 阻塞不会消耗cpu # 不停的询问连接是否建立好, 需要while循环不停的去检查状态 # 做计算任务或者再次发起其他的连接请求 client.send( "GET {} HTTP/1.1\r\nHost:{}\r\nConnection:close\r\n\r\n".format(path, host).encode("utf8")) # 通过\r\n 添加请求头信息 data = b"" while True: d = client.recv(1024) if d: data += d else: break data = data.decode("utf8") # data包含请求头信息和返回的响应数据 html_data = data.split("\r\n\r\n")[1] # 通过\r\n\r\n 讲请求头信息和响应数据分开 print(html_data) client.close() if __name__ == "__main__": start_time = time.time() for url in range(20): url = "http://shop.projectsedu.com/goods/{}/".format(url) get_url(url) print(time.time() - start_time)