Case: multi task udp chat
Explain
Write a program with two threads
Thread 1 is used to receive data and display
Thread 2 is used to detect keyboard data and send data through udp
Requirement
Achieve the above requirements
Summarize the characteristics of multitask program
Reference code:
import socket import threading def send_msg(udp_socket): """Get keyboard data and send it to the other party""" while True: # 1. Input data from keyboard msg = input("\n Please enter the data to send:") # 2. Input the ip address of the other party dest_ip = input("\n Please input the other party's ip address:") # 3. Enter the port of the other party dest_port = int(input("\n Please input the other party's port:")) # 4. Send data udp_socket.sendto(msg.encode("utf-8"), (dest_ip, dest_port)) def recv_msg(udp_socket): """Receive data and display""" while True: # 1. Receiving data recv_msg = udp_socket.recvfrom(1024) # 2. Decoding recv_ip = recv_msg[1] recv_msg = recv_msg[0].decode("utf-8") # 3. Display received data print(">>>%s:%s" % (str(recv_ip), recv_msg)) def main(): # 1. Create socket udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 2. Bind local information udp_socket.bind(("", 7890)) # 3. Create a sub thread to receive data t = threading.Thread(target=recv_msg, args=(udp_socket,)) t.start() # 4. Let the main thread detect keyboard data and send it send_msg(udp_socket) if __name__ == "__main__": main()## Case: multi task udp chat
Explain
- Write a program with two threads
- Thread 1 is used to receive data and display
- Thread 2 is used to detect keyboard data and then send the data through udp
Requirement
- Achieve the above requirements
- Summarize the characteristics of multitask program
Reference code:
import socket import threading def send_msg(udp_socket): """Get keyboard data and send it to the other party""" while True: # 1 \. Enter data from keyboard msg = input("\n Please enter the data to send:") # 2 \. Enter the ip address of the other party dest_ip = input("\n Please input the other party's ip address:") # 3 \. Enter the port of the other party dest_port = int(input("\n Please input the other party's port:")) # 4 \. Send data udp_socket.sendto(msg.encode("utf-8"), (dest_ip, dest_port)) def recv_msg(udp_socket): """Receive data and display""" while True: # 1 \. Receive data recv_msg = udp_socket.recvfrom(1024) # 2 \. Decoding recv_ip = recv_msg[1] recv_msg = recv_msg[0].decode("utf-8") # 3 \. Display received data print(">>>%s:%s" % (str(recv_ip), recv_msg)) def main(): # 1 \. Create socket udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 2 \. Bind local information udp_socket.bind(("", 7890)) # 3 \. Create a sub thread to receive data t = threading.Thread(target=recv_msg, args=(udp_socket,)) t.start() # 4 \. Let the main thread detect keyboard data and send it send_msg(udp_socket) if __name__ == "__main__": main()
=======================================================
Note: the above content is from the learning notes of the wisdom transfer class. If you need to reprint or complete the notes, please contact me wechat.
Project Python updates my study notes every day. Please refer to the original link for more details
If you have better experience and suggestions, welcome to discuss together. Welcome search official account to join python developer exchange platform.