Ai
1 Star 0 Fork 0

eswindous/python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
通信.py 878 Bytes
一键复制 编辑 原始数据 按行查看 历史
eswindous 提交于 2025-05-11 11:31 +08:00 . 实验2
import socket
# 创建TCP套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定地址和端口
server_address = ('localhost', 8888)
server_socket.bind(server_address)
# 开始监听,最大连接数为5
server_socket.listen(5)
print(f"服务器正在监听 {server_address[0]}:{server_address[1]}...")
# 接受客户端连接
client_socket, client_address = server_socket.accept()
print(f"接受来自 {client_address} 的连接")
try:
while True:
# 接收客户端消息通信.py
data = client_socket.recv(1024)
if not data:
break
print(f"客户端说: {data.decode('utf-8')}")
# 发送响应给客户端
response = input("服务端回应: ")
client_socket.send(response.encode('utf-8'))
finally:
# 关闭连接
client_socket.close()
server_socket.close()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/eswindous/python.git
git@gitee.com:eswindous/python.git
eswindous
python
python
master

搜索帮助