美文网首页
python udp线程聊天

python udp线程聊天

作者: IthinkIcanfly | 来源:发表于2019-02-11 21:46 被阅读0次
    import socket
    import threading
    
    def send_data(udp_socket):
        while True:
            send_data = input("input")
            udp_socket.sendto(send_data.encode("utf-8"), ("127.0.0.1", 7788))
    
    def recv_msg(udp_socket):
        while True:
            recv_data = udp_socket.recvfrom(1024)
            print(recv_data)
    
    def main():
        udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        #绑定本地信息
        udp_socket.bind(("127.0.0.1", 7890))
    
        t1 = threading.Thread(target=recv_msg, args=(udp_socket,))
        t2 = threading.Thread(target=send_data, args=(udp_socket,))
    
        t1.start()
        t2.start()
    
    if __name__ == '__main__':
        main()

    相关文章

      网友评论

          本文标题:python udp线程聊天

          本文链接:https://www.haomeiwen.com/subject/bneceqtx.html