💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
#### 1. 任务要求: 使用多线程完成一个全双工的QQ聊天程序 #### 2. 运行现象如下 ![](https://box.kancloud.cn/2adab1d88535ced4f80728e9d5c9c9ef_1645x901.png) ~~~ from threading import Thread from socket import * #1. 收数据,然后打印 def recvData(): while True: recvInfo = udpSocket.recvfrom(1024) print(">>%s:%s"%(str(recvInfo[1]), recvInfo[0])) #2. 检测键盘,发数据 def sendData(): while True: sendInfo = input("<<") udpSocket.sendto(sendInfo.encode("gb2312"), (destIp, destPort)) udpSocket = None destIp = "" destPort = 0 def main(): global udpSocket global destIp global destPort destIp = input("对方的ip:") destPort = int(input("对方的ip:")) udpSocket = socket(AF_INET, SOCK_DGRAM) udpSocket.bind(("", 4567)) tr = Thread(target=recvData) ts = Thread(target=sendData) tr.start() ts.start() tr.join() ts.join() if __name__ == "__main__": main() ~~~