1 客户端
from socket import *from threading import Threadclass UdpClient(): def __init__(self, src_port, dst_host, dst_port): self.cli = socket(AF_INET, SOCK_DGRAM) self.cli.bind(('', src_port)) self.dst_host = dst_host self.dst_port = dst_port def send(self, msg: str): self.cli.sendto(msg.encode(), (self.dst_host, self.dst_port)) print('消息发送成功') def recv(self): try: data, addr = self.cli.recvfrom(1024) except: return response = data.decode() print('接收到来自{}的消息:{}'.format(addr, response))def t_read(cli: UdpClient): while True: print('正在等待消息...') cli.recv()def t_write(cli: UdpClient): while True: msg = input('输入要发的消息:') cli.send(msg)cli = UdpClient(8347, '192.168.3.67', 8367)t1 = Thread(target=t_read, args=(cli,))t2 = Thread(target=t_write, args=(cli,))t1.start()t2.start()t1.join()t2.join()
2 WireShark抓包
