python学习之UDP实现一个会话

from socket import socket,AF_INET,SOCK_DGRAM
import threading

udp_socket = socket(AF_INET,SOCK_DGRAM)
udp_socket.bind(("",8089))
addr = ("172.20.17.1",8080)

def send_msg():
    while True:
        data = input("send msg:")
        if data == "q":
            break
        udp_socket.sendto(data.encode("gb2312"), addr)

def rcv_msg():
    while True:
        rdata = udp_socket.recvfrom(1024)
        print(rdata)

if __name__=="__main__":
    t1 = threading.Thread(target=send_msg)
    t2 = threading.Thread(target=rcv_msg)
    print("begin")
    t1.start()
    t2.start()

    t1.join()
    t2.join()
    print("end")
    udp_socket.close()

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读