In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to achieve Udp to receive data", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to achieve Udp to receive data"!
Udp receives data, and the network debugging assistant acts as the sender, but what you need when sending is the ip and port of the virtual machine, and the ip can be obtained through config, so how to make a program have a fixed port?
When you create a socket, you don't have a port number, for example, if you buy a mobile phone without a cell phone number, others can't find you, so have a fixed port number and bind a port with udp_socket.bind ().
Next, write the program, in the terminal:
The vim 05 binding port is used to receive data .py
The idea of writing a program: if name== "main":
Main ()
Then def main ():
Write code in this function.
Use comments to build the process
# 1. Create a socket
# 2. Bind a local information
# 3. Receive data
# 4. Print the received data
# 5. Close the socket
After the process is built, you start to write code, and when you create a socket, you use socket.XX. At this point, you need to import socekt at the top of the code and guide what you need. The code is not written from the first line.
Because there are more contemporary codes, such as more than 200 lines, we are not sure what will be used, so import what is used and adjust what is used.
1. The udp binding port is used to receive data .py
The code is in the following form:
Import socket
Def main ():
# 1. Create a socket
Udp_socket = socket.socket (socket.AF_INET,socket.SOCK_DGRAM)
# 2. Bind a local message (udp ctrl n auto-completion). You must bind your own computer's ip port. Other computers cannot.
Bind a tuple, represented by the localaddr variable, local ip, bind port, port number greater than 1024
Localaddr = ("", 7788)
Udp_socket.bind (localaddr)
# 3. Receive data (send data is a socket. Sendto receive is a socket. Recvfrom (1024)
1024 represents the maximum number of bytes received this time), and receives it with the recv_data variable
Recv_data = udp_socket.recvfrom (1024)
# 4. Print the received data
Print (recv_data)
# 5. Close the socket
Udp_socket.close ()
If _ _ name__== "_ _ main__":
Main ()
After the code is completed, the python3 05 binding port in the terminal is used to receive data. py to run the program, and the message sent by the network debugging assistant can be received in the terminal. The received content is shown as follows:
The printed data, the variable recv_data, is a tuple, the first parameter is the sent data, and the second parameter is a small tuple, which is the sender's ip and port.
If you want to display only the received data in the terminal, but not the ip and port of the other party, do the following:
The cp 05 binding port is used to receive data .py 06 parses the received data .py
2. Parse the received data .py
About comments:
If you comment at the beginning of a line, write the comment directly with # followed by a space
If you are commenting the code after the code, add two spaces after the code, and then write the comment content with # followed by a space.
Ideas for printing code adjustments:
Print ("s%: s%",% (send_addr,recv_msg))
But send_addr is a tuple, so you can't use it directly. You need to use str (send_addr) to convert it.
Recv_msg cannot be used directly, because it is encoded into bite type when it is sent, and what is received is also bite type. If you send letters directly, you can receive them, but if you send Chinese characters and receive garbled codes, you need to decode them at this time.
Recv_msg.decode ("utf-8"), but at this time, it is still garbled to send Chinese code with windows's network debugging assistant. Because windows is not encoded in utf-8, it uses gbk encoding, so decoding needs to use gbk, that is, recv_msg.decode ("gbk").
If the network debugging assistant is put into ubantu, the decoding still needs to be solved by utf-8.
Import socket
Def main ():
# 1. Create a socket
Udp_socket = socket.socket (socket.AF_INET,socket.SOCK_DGRAM)
# 2. Bind a local information
Localaddr = ("", 7788)
Udp_socket.bind (localaddr)
# 3. Receive data
Recv_data = udp_socket.recvfrom (1024)
Recv_msg = recv_data [0] # used to receive sent content
Send_addr = recv_data [1] # used to receive ip and port of the sender
# 4. Print the received data
# print (recv_data) # the printed code needs to be commented out and readjusted
Pint ("s% send_addr",% (str (send_addr), recv_msg.decode ("gbk")
# 5. Close the socket
Udp_socket.close ()
If _ _ name__== "_ _ main__":
Main ()
* * Summary: http://www.87554006.com/ of Wuxi Gynecological examination Hospital
Send data steps:
1. Create a socket
two。 Send data
3. Close the socket
To receive data:
1. Create a socket
two。 Bind a local ip port
3. Receive data
4. Print socket
To cycle through the terminal to receive data all the time, do the following:
Cp 06 parses the received data .py 07 loops to receive data .py
Vim 07 receives data .py in a loop
3. Receive data in a loop .py
If you want to receive in a loop, use while true to while true the receiving and printing code:
ESC large V up and down arrow selected, greater than sign indented to the right
Import socket
Def main ():
# 1. Create a socket
Udp_socket = socket.socket (socket.AF_INET,socket.SOCK_DGRAM)
# 2. Bind a local information
Localaddr = ("", 7788)
Udp_socket.bind (localaddr)
# 3. Receive data
While True:
Recv_data = udp_socket.recvfrom (1024)
Recv_msg = recv_data [0] # used to receive sent content
Send_addr = recv_data [1] # used to receive ip and port of the sender
# 4. Print the received data
Pint ("s% send_addr",% (str (send_addr), recv_msg.decode ("gbk")
# 5. Close the socket
Udp_socket.close ()
If _ _ name__== "_ _ main__":
Main ()
At this point, I believe you have a deeper understanding of "how to achieve Udp to receive data", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.