python socket服务端
import socket # 创建 socket 对象 server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 绑定 IP 地址和端口号 server_socket.bind(('127.0.0.1', 8000)) # 监听连接 server_socket.listen(1) print('等待客户端连接...') while True: # 接受客户端连接请求,并返回连接对象和客户端地址 client_socket, client_address = server_socket.accept() print(f'客户端 {client_address} 已连接') # 处理客户端请求,并发送响应数据 request = client_socket.recv(1024) response = b'Hello, world!' client_socket.sendall(response) # 关闭连接 client_socket.close()
###
标签:socket,python,server,client,连接,服务端,客户端 From: https://www.cnblogs.com/herd/p/17962696