Home >Backend Development >Python Tutorial >Introduction to socket implementation of TCP communication in python (with examples)
This article brings you an introduction to the implementation of TCP communication through sockets in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.
TCP
TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol, developed by the IETF Definition of RFC 793. In the simplified OSI model of computer networks, it completes the functions specified by the fourth layer transport layer. User Datagram Protocol (UDP) is another important transport protocol within the same layer [1]. In the Internet protocol suite, the TCP layer is an intermediate layer located above the IP layer and below the application layer. Reliable, pipe-like connections are often required between application layers of different hosts, but the IP layer does not provide such a flow mechanism, but provides unreliable packet switching.
The following is a schematic diagram of socket realizing TCP communication. We write the program according to the schematic diagram
TCP server
The server process must first bind a port and listen for connections from other clients. If a client connects, the server establishes a Socket connection with the client, and subsequent communications rely on this Socket connection.
Let's write a simple server program that receives client connections and replies to requests from clients.
import socket server = socket.socket() server.bind(('192.168.1.165',8900)) #调用 listen() 方法开始监听端口, 传入的参数指定等待连接的最大数量 server.listen(4) serObj,address = server.accept() #当有客户端访问时,实现两边的交流,如果有一方退出,整个程序退出。 #服务器程序通过一个永久循环来接受来自客户端的连接 #这里虽然给出最大连接数为4,但单线程程序也只会响应一个连接 while True: #建立连接后,服务端等待客户端发送的数据,实现通信 re_data = serObj.recv(1024).decode('utf-8') print('client>>',re_data) if re_data == 'quit': break send_data = input('server>>') serObj.send(send_data.encode('utf-8')) if send_data == 'quit': break serObj.close() server.close()
TCP Client
Most connections are reliable TCP connections. When a TCP connection is created, the one who initiates the connection is called the client, and the one who is
initiated to respond to the connection is called the server.
To actively initiate a TCP connection, the client must know the IP address and port number of the server.
import socket client = socket.socket() client.connect(('192.168.1.165',8900)) while True: send_data = input("client>>") client.send(send_data.encode('utf-8')) if send_data == 'quit': break re_data = client.recv(1024).decode('utf-8') if re_data == 'quit': break print("server>>",re_data) client.close()
Use one window to run the client and another window to run the server, so you can see the effect more intuitively.
Here my 08_pra.py is the server program and 09_pra.py is the client program
Schematic diagram when the connection is just established
One communication Completed diagram
The above is the detailed content of Introduction to socket implementation of TCP communication in python (with examples). For more information, please follow other related articles on the PHP Chinese website!