2020-01-15 udp

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "ryan"
# Date: 2020/1/15
import socket
import threading

def send_udp(udp_socket,dest_ip,dest_port):
    while True:
        data = input("输入要发送的数据:")
        udp_socket.sendto(data.encode('utf-8'), (dest_ip, dest_port))


def receive_udp(udp_socket):
    while True:
        data,_= udp_socket.recvfrom(1024)
        print(data.decode('gbk'))


def main():
    # dest_ip = input("请输入目标ip")
    # dest_port = int(input("请输入目标端口"))
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udp_socket.bind(("", 7890))
    dest_ip = "192.168.5.1"
    dest_port = 8081
    t1 = threading.Thread(target=send_udp ,args=(udp_socket,dest_ip,dest_port))
    t2 = threading.Thread(target=receive_udp, args=(udp_socket,))

    t2.start()
    t1.start()

if __name__ == '__main__':
    main()


你可能感兴趣的:(2020-01-15 udp)