Python全棧__服務器與多客戶端通信、UDP協議、pycharm打印帶顏色輸出、時間同步的機制
阿新 • • 發佈:2018-06-06
enc bre list print bsp sock pan lis acc
1、服務器與多客戶端通信
1 import socket 2 # 創建tcp socket的套接字 3 sk = socket.socket() 4 # bind 5 sk.bind((‘127.0.0.1‘,8080)) 6 # 監聽 7 sk.listen(5) 8 # 等待連接 9 while 1: 10 conn, addr = sk.accept() 11 while 1: 12 msg_s = input(‘>>>‘) 13 conn.send(msg_s.encode(‘utf-8TCP_服務器端‘)) 14 if msg_s == ‘q‘: 15 break 16 msg_r = conn.recv(1024).decode(‘utf-8‘) 17 if msg_r == ‘q‘: 18 break 19 print(msg_r) 20 conn.close() 21 22 sk.close()
1 import socket 2 3 sk = socket.socket() 4 5 sk.connect((‘TCP_客戶端127.0.0.1‘,8080)) 6 7 8 while 1: 9 msg_r = sk.recv(1024).decode(‘utf-8‘) 10 if msg_r == ‘q‘: 11 break 12 print(msg_r) 13 msg_s = input(‘>>>‘) 14 sk.send(msg_s.encode(‘utf-8‘)) 15 if msg_s == ‘q‘: 16 break 17 18 sk.close()
2、UDP協議
3、pycharm打印帶顏色輸出
4、時間同步的機制
Python全棧__服務器與多客戶端通信、UDP協議、pycharm打印帶顏色輸出、時間同步的機制