❥ you can't learn a series ❥ -- network programming based on Python

1, IP address

1. Concept of IP address

  • IP address is an address that identifies devices in the network, just like the home address in real life
  • Rendering of equipment in the network:

2. Expression of IP address

  • IP addresses fall into two categories: IPv4 and IPv6
  • IPv4 is the currently used ip address
  • IPv6 is the ip address to be used in the future
  • IPv4 is composed of dotted decimal
  • IPv6 is made up of colon hexadecimal

3. Role of IP address

  • IP address is used to identify the only device in the network, that is, a device in the network can be found through IP address
  • Effect drawing of IP address:

4. View IP address

  • Linux and mac OS use ifconfig
  • Windows uses the ipconfig command
  • Both ifconfig and ipconfig view the network card information, which includes the IP address of the device
  • 192.168.1.107 is the IP address of the device in the network
  • 127.0.0.1 indicates the local address. Prompt: you can use this address if you communicate with your own computer
  • 127.0.0.1 the domain name corresponding to the address is localhost. The domain name is the alias of the ip address. A corresponding ip address can be resolved through the domain name

5. Check whether the network is normal

  • Check whether the network uses the ping command normally
  • Check whether the network is normal. Rendering:
  • ping www.baidu.com to check whether you can access the public network
  • ping the ip address of the current LAN and check whether it is in the same LAN
  • ping 127.0.0.1 check whether the local network card is normal

2, Port and port number

1. What is a port

  • The port is the channel for data transmission, just like the door of the classroom. It is the only way for data transmission
  • So how to accurately find the corresponding port?
    In fact, each port has a corresponding port number. For example, each classroom door has a house number. If you want to find the port, you can use the port number

2. What is a port number

  • In order to uniformly manage so many ports, the operating system numbers the ports. This is the port number. The port number is actually a number, just like the house number in our real life
  • There are 65536 port numbers
  • Then the final data communication process is as follows: find the corresponding device through the ip address, find the corresponding port through the port number, and then transmit the data to the application through the port
  • Final communication flow rendering:

3. Classification of port number

  • Well known port number
  • Dynamic port number
  • Well known port number
  • Well known port numbers refer to well-known port numbers, ranging from 0 to 1023
  • These port numbers are generally assigned to some services. For example, port 21 is assigned to FTP (File Transfer Protocol) service, port 25 is assigned to SMTP (Simple Mail Transfer Protocol) service, and port 80 is assigned to HTTP service
  • Dynamic port number
  • The port number used by programmers to develop applications is called dynamic port number, which ranges from 1024 to 65535
  • If the program developed by the programmer does not set the port number, the operating system will randomly generate one within the range of dynamic port number for the developed application
  • Running a program will have a port number by default. When the program exits, the occupied port number will be released

3, TCP

1. Concept of TCP

  • TCP's transmission control protocol is abbreviated as transmission control protocol. It is a connection oriented, reliable and byte stream based transport layer communication protocol
  • Connection oriented renderings:

2.TCP communication steps

  • 1. Create a connection
  • 2. Data transmission
  • 3. Close the connection
  • TCP communication model is equivalent to 'making a phone call' in life. Before communication starts, you must establish a connection before sending data. At the end of communication, you must close the connection

3. Characteristics of TCP

  • Connection oriented
    • Both communication parties must establish a connection before data transmission. After data transmission, both parties must disconnect this connection to release system resources
  • Reliable transmission
    • TCP adopts send reply mechanism
    • Timeout retransmission
    • Error check
    • Flow control and congestion management

4, socket

  • Socket (socket for short) is a tool for network data communication between processes, just like the socket in real life. All household appliances need to work based on the socket, and network communication between processes needs to be based on this socket
  • socket rendering:

5, TCP network application development process

  • TCP network application development is divided into:
    • TCP client program development
    • TCP server program development
  • Client program refers to the program running on user equipment
  • Server program refers to the program running on the server device, which provides data services for the client
  • TCP client program development process


Step description:

  • Create client socket object
  • Establish connection with server socket
  • send data
  • receive data
  • Close Client Socket
  • TCP server program development process


Step description:

  • Create server socket object
  • Binding port number
  • Set listening
  • Waiting to accept the connection request from the client
  • receive data
  • send data
  • Close socket

6, TCP client program development

1. Introduction to socket class

  • import socket module
  • Create the client socket object socket.socket(AddressFamily, Type)
  • Parameter Description:
    • AddressFamily refers to the IP address type, which is divided into IPv4 and IPv6
    • Type indicates the transport protocol type
  • Method description:
    • connect((host, port)) means to establish a connection with the server socket. Host is the server ip address and port is the port number of the application
    • send(data) means send data, and data is binary data
    • Recv (buffer size) indicates the received data, and buffer size is the length of each received data

2.TCP client program example code

import socket


if __name__ == '__main__':
    # Create tcp client socket
    # 1. AF_INET: indicates ipv4
    # 2. SOCK_STREAM: tcp transport protocol
    tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # Establish connection with server application
    tcp_client_socket.connect(("192.168.131.62", 8080))
    # When the code is executed to this point, the connection is established successfully
    # Ready to send data
    send_data = "Hello, server, I'm client Xiaohei!".encode("gbk")
    # send data
    tcp_client_socket.send(send_data)
    # Receive data. The maximum number of bytes of data received this time is 1024
    recv_data = tcp_client_socket.recv(1024)
    # The returned data is directly the binary data sent by the server program
    print(recv_data)
    # Decode data
    recv_content = recv_data.decode("gbk")
    print("The data of the receiving server is:", recv_content)
    # Close socket
    tcp_client_socket.close()

# results of enforcement
b'hello'
The data of the receiving server is: hello

7, TCP server program development

1. Introduction to socket class

  • import socket module
  • Create the server socket object socket.socket(AddressFamily, Type)
  • Parameter Description:
    • AddressFamily refers to the IP address type, which is divided into IPv4 and IPv6
    • Type indicates the transport protocol type
  • Method description:
    • bind((host, port)) indicates the binding port number. Host is the ip address and port is the port number. Generally, the ip address is not specified, indicating that any ip address of the machine can be used
    • listen (backlog) indicates setting listening. The backlog parameter indicates the maximum number of connections waiting to be established
    • accept() means waiting to accept the connection request from the client
    • send(data) means send data, and data is binary data
    • Recv (buffer size) indicates the received data, and buffer size is the length of each received data

2.TCP server program example code

import socket

if __name__ == '__main__':
    # Create tcp server socket
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # Set the port number reuse, let the program exit, and release the port number immediately
    tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) 
    # Bind port number to program
    tcp_server_socket.bind(("", 8989))
    # Set listening
    # 128: the maximum number of connections waiting to be established. Prompt: at present, it is a single task server, which can only serve one client at a time. The subsequent use of multitasking can enable the server to serve multiple clients at the same time,
    # There is no need for the client to wait to establish a connection
    # The socket after listen is only responsible for receiving client connection requests and cannot send and receive messages. The new socket returned is used to send and receive messages
    tcp_server_socket.listen(128)
    # Wait for the client to establish a connection. The code will unblock and continue to execute only after the client and server establish a connection successfully
    # 1. Socket dedicated to communication with client: service_client_socket
    # 2. ip address and port number of the client: ip_port
    service_client_socket, ip_port = tcp_server_socket.accept()
    # This code indicates that the connection is established successfully
    print("Client ip Address and port number:", ip_port)
    # Receive the data sent by the client. The maximum number of bytes of data received this time is 1024
    recv_data = service_client_socket.recv(1024)
    # Gets the length of the data
    recv_data_length = len(recv_data)
    print("The length of the received data is:", recv_data_length)
    # Decoding binary data
    recv_content = recv_data.decode("gbk")
    print("The data received from the client is:", recv_content)
    # Ready to send data
    send_data = "ok, The problem is being handled...".encode("gbk")
    # Send data to client
    service_client_socket.send(send_data)
    # Close the socket between the service and the client, and terminate the service communicating with the client
    service_client_socket.close()
    # Close the socket of the server and terminate the service of establishing connection request with the client
    tcp_server_socket.close()

# results of enforcement
 Client ip Address and port number: ('172.16.47.209', 52472)
The length of the received data is: 5
 The data received from the client is: hello

8, Considerations for TCP network applications

  • When the TCP client program wants to communicate with the TCP server program, it must first establish a connection
  • TCP client programs generally do not need to bind the port number, because the client initiates the connection actively
  • The TCP server program must bind the port number, otherwise the client cannot find the TCP server program
  • The socket after listen is a passive socket, which is only responsible for receiving connection requests from new clients and cannot send and receive messages
  • When the TCP client program and the TCP server program are connected successfully, the TCP server program will generate a new socket, which will be used for sending and receiving client messages
  • Closing the socket returned by accept means that communication with the client has been completed
  • Closing the socket after listen ing means that the socket of the server is closed, which will cause new clients to be unable to connect to the server, but the previously successfully connected clients can still communicate normally
  • When the socket of the client calls close, the recv of the server will unblock and the length of the returned data is 0. The server can judge whether the client has been offline by the length of the returned data. On the contrary, when the server closes the socket, the recv of the client will unblock and the length of the returned data is 0

9, Case list - multitasking TCP server program

1. Demand

  • The previous TCP server program can only serve one client. How to develop a multitasking version of TCP server program that can serve multiple clients?
  • When multitasking is completed, threads can be used to save memory resources more than processes

2. Implementation steps

  • Write a TCP server program and wait for the client to accept the connection request
  • When the connection between the client and the server is established successfully, a sub thread is created, and the sub thread is used to process the client's request to prevent the main thread from blocking
  • Set the created sub thread as the guardian main thread to prevent the main thread from exiting

3. Example code of multitasking TCP server program

import socket
import threading


# Handle the requested operation of the client
def handle_client_request(service_client_socket, ip_port):
    # Loop receiving data sent by the client
    while True:
        # Receive data sent by client
        recv_data = service_client_socket.recv(1024)
        # if statements can be directly used to judge whether there is data in the container type. if there is data in the container type, the condition is valid, otherwise the condition fails
        # Container type: list, dictionary, tuple, string, set, range, binary data
        if recv_data:
            print(recv_data.decode("gbk"), ip_port)
            # reply
            service_client_socket.send("ok,The problem is being handled...".encode("gbk"))

        else:
            print("The client is offline:", ip_port)
            break
    # Terminate communication with client
    service_client_socket.close()


if __name__ == '__main__':
    # Create tcp server socket
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # Set the port number reuse, let the program exit, and release the port number immediately
    tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
    # Binding port number
    tcp_server_socket.bind(("", 9090))
    # Set listening. The socket after listening is a passive socket and is only responsible for receiving connection requests from clients
    tcp_server_socket.listen(128)
    # Cycle waiting to receive the connection request from the client
    while True:
        # Waiting to receive the connection request from the client
        service_client_socket, ip_port = tcp_server_socket.accept()
        print("Client connection succeeded:", ip_port)
        # When the connection between the client and the server is successful, a sub thread needs to be created, and different sub threads are responsible for receiving messages from different clients
        sub_thread = threading.Thread(target=handle_client_request, args=(service_client_socket, ip_port))
        # Set daemon main thread
        sub_thread.setDaemon(True)
        # Start child thread
        sub_thread.start()
    # The tcp server socket does not need to be closed because the server program needs to be running all the time
    # tcp_server_socket.close()

# results of enforcement
 Client connection succeeded: ('172.16.47.209', 51528)
Client connection succeeded: ('172.16.47.209', 51714)
hello1 ('172.16.47.209', 51528)
hello2 ('172.16.47.209', 51714)

10, Analysis of send and recv principles of socket

1. Send and receive buffer of socket

When creating a TCP socket object, there will be a send buffer and a receive buffer. The send and receive buffer refers to a piece of space in memory

2. Analysis of send principle

  • Does send send send data directly to the server?
  • No, if you want to send data, you must send data through the network card. The application cannot send data directly through the network card. It needs to call the operating system interface, that is, the application writes the sent data to the sending buffer (a space in memory), and then the operating system controls the network card to send the data in the sending buffer to the server network card

3. Analysis of recv principle

  • Does recv receive data directly from the client?
  • No, the application software cannot directly receive data through the network card. It needs to call the operating system interface. The operating system receives data through the network card, writes the received data to the receiving buffer (a space in memory), and then the application obtains the data sent by the client from the receiving buffer

4. Schematic diagram of send and recv


Neither recv nor send directly receives the data from the other party and sends the data to the other party. The sending data will be written to the sending buffer. The receiving data is read from the receiving buffer. The sending data and receiving data are finally completed by the operating system control network card

Keywords: Python Linux websocket udp TCP/IP

Added by Anarking on Sun, 19 Sep 2021 14:14:08 +0300