import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 基于tcp/ip建立连接 sock.bind(('192.168.1.108', 8000)) # 监听本机8000 sock.listen(5) # 最大连接数 while True: # 进入死循环,等待浏览器访问 conn, addr = sock.accept() data = conn.recv(1024) # 接收来自浏览器的请求给data print(data) # 给浏览器返回的内容 conn.send(b"HTTP/1.1 200 OK Content-Type:text/html; charset=utf-8 ") conn.send("电脑前的你长得真好看!".encode("utf-8")) # 关闭和浏览器创建的socket连接 conn.close() if __name__ == "__main__": main()