赞
踩
import socket
# 创建一个socket对象
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 获取本地主机名
host = socket.gethostname()
# 设置一个端口
port = 12345
# 绑定端口
server_socket.bind((host, port))
# 设置最大连接数,超过后排队
server_socket.listen(5)
while True:
# 接受一个新连接
client_socket, addr = server_socket.accept()
print(f"连接地址: {addr}")
message = '服务器响应!' + "\r\n"
client_socket.send(message.encode('ascii'))
client_socket.close()
# 关闭服务器socket
server_socket.close()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。