美文网首页
2020-01-15 udp

2020-01-15 udp

作者: 培根好吃 | 来源:发表于2020-01-15 10:53 被阅读0次
    #! /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

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