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 make simple chatter and build UDP Network Communication Model by Python

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

Share

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

This article will explain in detail how to make a simple chatter and build a UDP network communication model in Python. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Foreword:

What is the nature of the Internet? In fact, it is the exchange of information. Such as our commonly used QQ, Wechat and so on. So how do you send your own information to other people's computers?

Then you need to use the network model to accomplish such a thing. Today we are going to use the UDP network model to complete a simple chatter.

Share content:

Implementation of socket in python

How to establish communication with Ubuntu

Send / receive messages

Environment:

Windows

Ubuntu

Python3

Pycharm

1. Import module import socket

Socket is an object that sockets, links to a computer and receives / sends messages.

There are two types of socket: udp and tcp

Udp is relatively simple and fast in the case of unsafe packet loss.

The security speed of the complex network model of tcp is slow.

two。 Create a socket object def main (): udp_socket = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)

Udp needs the help of the network: IP protocol

There are two IP protocols: IPv4 and IPv6.

The PC computer adopts IPv4,IPv6, which is generally used by mobile: mobile phone.

3. Send data to udp_socket.sendto (b'hello python', ('192.168.3.40), 8080) udp_socket.close () 4 in ubuntu system. Send any data to def main (): udp_socket = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) send_data = input ('Please enter any character:') udp_socket.sendto (send_data.encode ('utf-8'), (' 192.168.3.40, 8080)) udp_socket.close () 5. Loop data def main (): # create socket udp_socket = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) # Loop send while True: send_data = input ('Please enter the data you want to send:') udp_socket.sendto ('utf-8'), (' 192.168.3.40' 8080)) if send_data = "exit": break udp_socket.close () 6. Accept data in a loop

Bind local information and tell ubuntu my current address and port

Def main (): udp_socket = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) localhost_addr = (', 6666) udp_socket.bind (localhost_addr) Loop receive while True: recv_data = udp_socket.recvfrom (1024) # tuple unpack recv_msg, recv_addr = recv_data print (recv_msg.decode ('utf-8'), recv_addr) extract the related functions into a function

Send a message:

Def send_msg (udp_socket): msg = input ('Please enter the message you want to send:') udp_socket.sendto (msg.encode ('utf-8'), (' 192.168.3.40 messages, 8080))

Receive messages:

Def recv_msg (udp_socket): # receive msg = udp_socket.recvfrom (1024) # Decode recv_msg = msg [0] .decode ('utf-8') print (recv_msg)

Send / receive function:

Def main (): udp_socket = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) udp_socket.bind ((', 6666)) while True: op_num = input ('Please enter the function serial number to be operated [1. Send message 2. Receive message]:') # call the related function if op_num = "1": send_msg (udp_socket) elif op_num = = "2": recv_msg (udp_socket) else: print ('input error, please re-enter.') 7. Start if _ _ name__ = ='_ _ main__': main () the last run code

Send a message:

Receive messages:

About Python how to make simple chatter and build UDP network communication model to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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