Python implements udp network program, sending and receiving data

The Internet protocol suite supports a connectionless transmission protocol, which is called the User Datagram Protocol (UDP, User Datagram Protocol).

UDP provides a way for applications to send encapsulated IP packets without establishing a connection.

udp network program model diagram.

udp send information

Next, we use python code to send and receive udp data.

One, udp network program, send data

Process:

  1. Create client socket
  2. send data
  3. Close socket
import socket

def main():
    # 1.创建一个udp套接字
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # 2.准备接收方的地址
    # 192.168.65.149 表示目的地ip
    # 30000  表示目的地端口
    udp_socket.sendto("hello".encode("utf-8"), ("192.168.65.149", 30000))

    # 3.关闭套接字
    udp_socket.close()


if __name__ == "__main__":
    main()

The first parameter of sendto needs a byte type, so it needs to be encoded. You can't just pass a string.

In order to verify whether the written program is correct, we can use the network debugging assistant to verify.

Network debugging assistant

In the above example, the "local host address" in the network debugging assistant should be filled in as the ip address of the server computer. We are testing on a computer here, so the ip address in the code is written.

But everyone should understand it as the ip of another server's computer.

Choose a port with a larger number instead of occupying the mainstream port.

Two, udp network program, receive data

Process:

  1. Create socket
  2. Bind local own information
  3. Receive data
  4. Close socket

The most important step in receiving data is to bind ports.

import socket

def main():
    # 1.创建一个udp套接字
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # 2.绑定本地的相关信息,如果一个网络程序不绑定,则系统会随机分配
    # 30000  表示本地的端口 ip一般不用写
    local_addr = ("", 30000)
    udp_socket.bind(local_addr)

    # 3. 等待接收对方发送的数据
    recv_data = udp_socket.recvfrom(1024)  
    # 1024表示本次接收的最大字节数

    # 6. 显示对方发送的数据
    # 接收到的数据recv_data是一个元组
    # 第1个元素是对方发送的数据
    # 第2个元素是对方的ip和端口
    print(recv_data[0].decode('gbk'))
    print(recv_data[1])

    # 3.关闭套接字
    udp_socket.close()


if __name__ == "__main__":
    main()

When decoding the received data, pay attention to the same format as the client-side encoding.

Care must be taken when choosing UDP as the transport protocol.

In an environment where the network quality is very unsatisfactory, UDP protocol packet loss will be more serious.

But due to the characteristics of UDP: it is not a connection type protocol, so it has the advantages of low resource consumption and fast processing speed, so usually audio, video and ordinary data use UDP more when transmitting, because they lose one or two data occasionally Packets will not have much influence on the reception result.

For example, the ICQ and QQ we use for chat are the UDP protocols used.


The pilgrimage of programming

Guess you like

Origin blog.csdn.net/beyondamos/article/details/108526824