I recently wrote a project for operation and maintenance, so I studied Python's TCP communication (a script is attached to the server, and a script is attached to the computer, and the communication can be encoded and encrypted, and then the desired content can be transmitted and retrieved) on the server side : from socket import * # Create socket tcp_server_socket = socket(AF_INET, SOCK_STREAM) # Local information address = ('127.0.0.1', 7788) # Bind tcp_server_socket.bind(address) # Default properties of socket created using socket It is active, use listen to make it passive, so that you can receive other people's links. The number in the listen indicates the degree to which the client can be connected at the same time. tcp_server_socket.listen(128) # If there is a new client to connect to the server , then a new socket is generated to serve this client # client_socket is used to serve this client # tcp_server_socket can be saved to wait for other new client links # clientAddr is a tuple (ip, port) client_socket, clientAddr = tcp_server_socket.accept() # Receive the data sent by the other party, and only the data returned is different from udp recv_data = client_socket.recv(1024) # Receive 1024 bytes print('The received data is:', recv_data.decode('gbk')) # Send some data to the client client_socket.send("thank you !".encode('gbk')) # # Close for this client As long as the socket of the terminal service is closed, it means that it can no longer serve the client. If the service is still needed, it can only be reconnected again client_socket.close()
client:
from socket import * # Create socket tcp_client_socket = socket(AF_INET, SOCK_STREAM) # Purpose information server_ip = input("Please enter server ip:") server_port = int(input("Please enter server port:")) # Link server tcp_client_socket. connect((server_ip, server_port)) # Prompt the user to input data send_data = input("Please enter the data to be sent:") tcp_client_socket.send(send_data.encode("gbk")) # Receive the data sent by the other party, the maximum reception 1024 bytes recvData = tcp_client_socket.recv(1024) print('The received data is:', recvData.decode('gbk')) # Close the socket tcp_client_socket.close()