代码:
from socket import * from threading import Thread #1.收数据,然后打印 def recvDate(): while True: recvInfo = udpSocket.recvfrom(1024) print("") print(">>%s:%s"%(str(recvInfo[1]),recvInfo[0].decode("gb2312"))) #2.检测键盘,发数据 def sendDate(): 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("对方的端口:")) udpSocket = socket(AF_INET,SOCK_DGRAM) udpSocket.bind(("",4567)) tr = Thread(target=recvDate) ts = Thread(target=sendDate) tr.start() ts.start() tr.join() ts.join() if __name__ == "__main__": main()
原文:https://www.cnblogs.com/fjfan/p/10794199.html