Employment DAY2_ chat network programming _udp - User can control

Import Socket 

DEF send_msg (UDP_SOCKET): 
    "" " Send Message " "" 
    # retransmission data 
    # acquires content to be transmitted to 
    the send_data = INPUT ( " Please enter the information transmission " ) 
    DEST_IP = INPUT ( " Please enter each other's IP; " ) 
    dest_port = int (iNPUT ( " Please enter the other's Port: " )) 
    udp_socket.sendto (send_data.encode ( " GBK " ), (DEST_IP, dest_port)) 


DEF recv_msg (UDP_SOCKET): 
    # receiving data and display 
    recv_data = UDP_SOCKET.recvfrom(1024 )
    #print(recv_data)
    # Recv_message = recv_data [ 0 Data] # stores the received 
    # send_address = recv_data [ . 1 ] # storage sender address information 
    # Print ( " the received message is% S;% S " % (STR (send_address), recv_message. decode ( " GBK " ))) 
    Print ( " the received message is a S%;% S " % (STR (recv_data [ . 1 ]), recv_data [ 0 ] .decode ( " GBK " ))) 

DEF main (): 
    # create a socket 
    UDP_SOCKET = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) 
    # binding information
    udp_socket.bind (( "" , 50000 )) 
    # event loop to process
    the while True: 
        Print ( " ---- chat device ---- " ) 
        Print ( " . 1: Send Message " ) 
        Print ( " 2: receiving a message " ) 
        Print ( " 0: Exit " ) 
        OP = INPUT ( " Please input function " )
         IF OP == " . 1 " : 
            send_msg (UDP_SOCKET) 
        elif OP == " 2"" : 
            Recv_msg (UDP_SOCKET) 
        elif OP ==0 " :
             BREAK 
        the else : 
            Print ( " input errors, please re-enter " ) 


IF __name__ == " __main__ " : 
    main ()

 

Guess you like

Origin www.cnblogs.com/joycezhou/p/11439953.html