Tutorial on UDP network programming in Python

Source: Internet
Author: User
This article mainly introduces the tutorial on UDP network programming in Python. UDP programming is the basic knowledge of Python network programming. the sample code is based on Python2.x, you can refer to TCP to establish a reliable connection, and both parties can send data in the form of a stream. Compared with TCP, UDP is a connectionless protocol.

When using UDP, you do not need to establish a connection. you only need to know the IP address and port number of the other party to send data packets directly. However, I don't know if it can be reached.

Although UDP is not reliable in data transmission, its advantage is that it is faster than TCP, and UDP can be used for data that does not require reliable arrival.

Let's take a look at how to transmit data through the UDP protocol. Similar to TCP, UDP communication can be divided into clients and servers. The server must first bind the port:

S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) # bind Port: s. bind ('192. 0.0.1 ', 127 ))

When a Socket is created, SOCK_DGRAM specifies that the Socket type is UDP. The binding port is the same as TCP, but does not need to call the listen () method, but directly receives data from any client:

Print 'bind UDP on 9999... 'While True: # receive data: data, addr = s. recvfrom (1024) print 'received Ed from % s: % s. '% addr s. sendto ('Hello, % s! '% Data, addr)

The recvfrom () method returns data and the client address and port. after receiving the data, the server directly calls sendto () to send the data to the client using UDP.

Note that multithreading is saved here, because this example is very simple.

When the client uses UDP, it still creates a UDP-based Socket, and then directly sends data to the server through sendto () without calling connect:

S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) for data in ['Michael ', 'Tracy', 'Sara']: # send data: s. sendto (data, ('2017. 0.0.1 ', 9999) # receive data: print s. recv (1024) s. close ()

The recv () method is still called to receive data from the server.

Start the server and the client separately using two command lines. The results are as follows:

Summary

UDP is similar to TCP, but no connection is required. In addition, the UDP port bound to the server does not conflict with the TCP port, that is, UDP port 9999 and TCP port 9999 can be bound to each other.

Source code reference: https://github.com/michaelliao/learn-python/tree/master/socket

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.