![](https://i-blog.csdnimg.cn/blog_migrate/edc92f975d3f93bee7fea18dc2631b9a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4de8e39a9e1642301f03fa09e34fae7c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/43b840613871219c15a7cde4fe664f56.png)
import socket
socket_sever = socket.socket()
socket_sever.bind(("localhost",8888))
socket_sever.listen(1)
# result = socket_sever.accept()
# conn = result[0] #客户端和服务端的链接对象
# address = result[1] #客户端的地址信息
conn,address = socket_sever.accept()
#accept 是阻塞的 等待客户端链接 没有链接就等待
print(f"receive connect {conn} addreess{address}")
while True:
data = conn.recv(1024).decode("UTF-8")
print(f"receive data {data}")
print(f"input send message")
msg = input()
if msg == "exit":
break
conn.send(msg.encode("UTF-8"))
conn.close()
socket_sever.close()
import socket
socket_client = socket.socket()
socket_client.connect(("localhost",8888))
while True:
msg = input("input send message")
if msg == 'exit':
break
socket_client.send(msg.encode("UTF-8"))
receive_data = socket_client.recv(1024)
print(f"服务端回复的消息={receive_data.decode("UTF-8")}")
socket_client.close()