Python_服务器与多客户端通信、UDP协议、pycharm打印带颜色输出、时间同步的机制

时间:2022-10-28 10:23:04

1、服务器与多客户端通信

 import socket
# 创建tcp socket的套接字
sk = socket.socket()
# bind
sk.bind(('127.0.0.1',8080))
# 监听
sk.listen(5)
# 等待连接
while 1:
conn, addr = sk.accept()
while 1:
msg_s = input('>>>')
conn.send(msg_s.encode('utf-8'))
if msg_s == 'q':
break
msg_r = conn.recv(1024).decode('utf-8')
if msg_r == 'q':
break
print(msg_r)
conn.close() sk.close()

TCP_服务器端

 import socket

 sk = socket.socket()

 sk.connect(('127.0.0.1',8080))

 while 1:
msg_r = sk.recv(1024).decode('utf-8')
if msg_r == 'q':
break
print(msg_r)
msg_s = input('>>>')
sk.send(msg_s.encode('utf-8'))
if msg_s == 'q':
break sk.close()

TCP_客户端

2、UDP协议

  未完待续

3、pycharm打印带颜色输出

未完待续

4、时间同步的机制

未完待续