一、UDP编码
1、
import socket sk = socket.socket(type=socket.SOCK_DGRAM) while 1: msg_s = input('>>>') sk.sendto(msg_s.encode('utf-8'),('127.0.0.1',8090)) msg_r,addr = sk.recvfrom(1024) print(msg_r.decode('utf-8')) sk.close()
import socket sk = socket.socket(type=socket.SOCK_DGRAM)# udp协议 sk.bind(('127.0.0.1',8090)) # 收发 while 1: msg_r, addr = sk.recvfrom(1024)# 接收来自于哪里的消息 print(msg_r.decode('utf-8'),addr) msg_s = input(('>>>')) sk.sendto(msg_s.encode('utf-8'),addr)# 发给谁的消息 sk.close()
2、署名的udp
import socket sk = socket.socket(type=socket.SOCK_DGRAM)# udp协议 name = input('请输入您的名字:') # 收发 while 1: msg_s = input(('>>>')) info = name + ' : ' + msg_s sk.sendto(info.encode('utf-8'), ('127.0.0.1',8090)) # 发给谁的消息 msg_r,addr = sk.recvfrom(1024)# 接收来自于哪里的消息 print(msg_r.decode('utf-8')) sk.close()
import socket sk = socket.socket(type=socket.SOCK_DGRAM)# udp协议 sk.bind(('127.0.0.1',8090)) # 收发 while 1: msg_r,addr = sk.recvfrom(1024)# 接收来自于哪里的消息 print(msg_r.decode('utf-8')) msg_s = input(('>>>')) sk.sendto(msg_s.encode('utf-8'),addr)# 发给谁的消息 sk.close()
二、print() 颜色输出
import socket sk = socket.socket(type=socket.SOCK_DGRAM)# udp协议 name = input('请输入您的名字:') # 收发 while 1: msg_s = input(('>>>')) info = name + ' : ' + msg_s sk.sendto(info.encode('utf-8'), ('127.0.0.1',8090)) # 发给谁的消息 msg_r,addr = sk.recvfrom(1024)# 接收来自于哪里的消息 print(msg_r.decode('utf-8')) sk.close()
import socket sk = socket.socket(type=socket.SOCK_DGRAM)# udp协议 sk.bind(('127.0.0.1',8090)) # 需求: 根据每个客户端的名字,去加上颜色 dic = {'alex':'