Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to implement a multitasking version of udp chatter

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article "how to use Python to achieve a multitasking version of udp chat" article knowledge point most people do not understand, so Xiaobian summarized the following content for everyone, detailed content, clear steps, has a certain reference value, I hope you can read this article to gain something, let's take a look at this article "how to use Python to achieve a multitasking version of udp chat" article bar.

I. Case examples II. Case descriptions

Write a program with 2 threads.

Thread 1 is used to receive data and then display it.

Thread 2 is used to detect keyboard data and then send data through udp.

import socketimport threadingdef send_msg(udp_socket): """Get keyboard data and send it to the other party""" while True: # 1. Enter data from keyboard msg = input("\nPlease enter data to send:") # 2. Enter the IP address of the other party dest_ip = input("\n Please enter the IP address of the other party:") # 3. Enter the port of the other party dest_port = int(input("\n Please enter the port of the other party:")) # 4. transmit 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 a socket udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 2. Binding local information udp_socket.bind(("", 7890)) # 3. Create a child 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() The above is about "how to use Python to achieve a multitasking version of udp chat" The content of this article, I believe everyone has a certain understanding, I hope the content shared by Xiaobian is helpful to everyone, if you want to know more related knowledge content, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report