"Python" Python Network programming introduction + UDP Preliminary

Source: Internet
Author: User

Introduction to Network programming

sockets are a set of program interfaces for network communication between computers. At present, socket programming has become the standard of network programming, communication between computers must comply with the requirements of the socket interface.

The socket object is the basis for network communication, which is the equivalent of a pipeline connecting the sending and receiving ends and passing data between the two. Python encapsulates the socket two times , simplifying program development steps and greatly improving the efficiency of the development.

In the network communication model, two important information is required to complete the transmission of the data:IP address and port number . On the receiving side, the host is responsible for receiving all the packets sent to itself, and put the data on the operating system data transmission line, and then by the corresponding application on the transmission line according to the port number of the respective data to be taken separately. This requires the application to wait on the line before the data arrives.

Once data arrives on the transport bus, these applications take their own data according to the port markings on the packet. However, if the data arrives at the transport bus and the application is not started, the data will be discarded by the operating system.

The above content shows:
① network packets need to have the recipient's information, namely the IP address and port number
② recipient must be ready to receive data before the data arrives
③ different applications, relying on different port numbers to identify their own data.

Introduction to TCP/IP protocol clusters

In fact, data travels over the Internet with a lot of processing, including a series of processes such as filling in the address of a message you send, packaging and converting it to an electrical signal. The receiver then converts the electrical signal into a packet, then unpacking it, and finally sees the original message being sent.

These complex processes are accomplished through the TCP/IP protocol cluster, which contains a series of basic protocols for network communication. Each protocol accomplishes different functions.
The TCP/IP reference model is divided into 5 tiers:
application layer, Transport layer, network layer, data link layer and physical layer
Only the same layer of hardware or software at the time of communication can identify data to each other, that is, the application layer only talks to the application layer, does not occur on one side is the application layer and the other is the transport layer of the conversation.

In the TCP/IP reference Model, theIP address is a feature of the network layer, and the port is the feature of the application layer.
The data is added to the additional information as it passes through each layer. This information is called the head. The process of joining the head is called the encapsulation of the data.
The transport layer joins a TCP header (including the port number)
The network layer is added to the IP header
The data link layer is added to the frame head and tail
The role of the data link layer is to be responsible for transmitting data from one end of the link to the other end of the link.

Preliminary programming of UDP network

There are two ways to transfer data in the network, no data loss is the TCP transmission, the data loss is the UDP transmission.
In most cases, reliable TCP is used in the network.

In order to communicate, both the sender and the receiver must create a socket object. The sender uses the socket to send data to the network, the receiving end reads the data from the socket, and then displays it on the screen with the print function.

Receive-side code

socketssocket.socket(socket.AF_INET,socket.SOCK_DGRAM)s.bind(("",15001s.recvfrom(1024)print(data)s.close()

Send-side code

socketsocket.socket(socket.AF_INET,socket.SOCK_DGRAM)s.sendto((‘Hello!!‘).encode(),(‘192.168.1.224‘,15001))s.close()
The code interprets the sending side

The socket module contains a variety of functions required for network communication, which encapsulates the complex network communication process, greatly simplifies the development and improves the development efficiency.

s = Socket.socket (socket.af_inet,socket. SOCK_DGRAM)
Create a socket object with two parameters:
The first one represents the protocol family family.
The second represents the type of protocol.
The return value of the function is a socket object. If the object is created successfully, you can use S to send and receive data.

SendTo () is a function that sends data. The first parameter is the string that needs to be sent, the second parameter is a tuple, the first element is the IP address, and the second is the port.

Receiving end

The same parameters should be used when calling the socket.
The bind () function is used to bind the socket to the specified port. The bind () function has two parameters, the first is the IP address, and the second is the port. The first parameter can use an empty string that represents any available IP that uses this computer. The second parameter makes the listening port.

In network communication, a port of a computer can only be used by 1 applications, and the available port range is 1-65536. of which 1-1024 of the ports are mostly used by the operating system and common software.

The Recvfrom () function is used to receive data from the network. Must be called after the bind () function. The combination of both is to listen on the specified port.
The first return value of Recvfrom () is the received data, and the second return value is the sender's IP address.
Recvfrom () has 1 parameters to specify the size of the receive buffer.

The sending side is also known as the client, and the receiving end is also known as the server side.

UDP function Introduction Socket

The first parameter of the socket:
Socket.af_inet IPv4
Socket.af_inet6 IPV6

The second parameter of the socket
Sock_stream TCP protocol
SOCK_DGRAM UDP protocol

SendTo

Socket.sendto (string,address)
Sends a string to the host specified in address.

Recvfrom

Socket.recvfrom (bufsize)
The UDP packet is received from the network and the received data is stored in the memory space defined by the bufsize. If the value of bufsize is less than the size of the received packet, the data will overflow.

"Python" Python Network programming introduction + UDP Preliminary

Related Article

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.