# socketserver """ 1、 Class call , Realization socket function 2、server End : Server side , Listen on the specified port , Provide services 3、client End : client , Connect server , Access to services """
Server side
# Introduce modules import socketserver # The implementation based on socket signal communication """ 1、 establish Mysocket class , The parent class is socketserver.BaseRequestHandler 2、 Override parent method handle 3、 call Mysocket It's actually execution handle Method 4、serve_forever() It will enter the blocking state after the end of the session , Wait for next connection """ class Mysocket(socketserver.BaseRequestHandler): # Override parent method handle def handle(self): # Access channel information conn = self.request # Get client information client_address = self.client_address # Print channel information print(conn) # Print client information print(client_address) # Receive print messages info = str(conn.recv(1024), encoding='utf8') print(info) # Send a message conn.sendall(bytes('hello too', encoding='utf8')) # Close the connection conn.close() if __name__ == '__main__': # Instantiation socket object server = socketserver.ThreadingTCPServer(('127.0.0.1', 8088), Mysocket) # initialization socket Connect , Enter the waiting state # serve_forever() It will enter the blocking state after the end of the session , Wait for next connection server.serve_forever()
client
# Introduce modules import socket # initialization ( Instantiation )socket object sk sk = socket.socket() # Define the server side IP port , Tuple format address = ('127.0.0.1', 8088) # Connect to server sk.connect(address) # Sending and receiving information """ 1、 Either the server or the client can send messages first 2、 It must be sent and received one time , Both ends cannot send or receive messages at the same time """ # Send a message """ 1、 The sending message must be bytes type 2、send: send out TCP data , take string The data in is sent to the connected socket . The return value is the number of bytes to send , The quantity may be less than string Byte size of ( It is possible that the transmission is incomplete ). 3、sendall: Send in full TCP data , Send in full TCP data . take string The data in is sent to the connected socket , But try to send all the data before returning . Successfully returns None, If you fail, throw an exception . 4、 The client sends information using socket object sk, The server side uses channels conn """ sk.sendall(bytes('hello', encoding='utf8')) # Receiving information """ 1、 Receive can specify the maximum number of bytes received at a time 2、 The information received is bytes type 3、 While waiting for reception , Go into blocking mode , Until the message is sent 4、 There is a size limit for receiving messages at one time , That is to say, it may not be able to receive all at once , It takes several times 5、 The client receives information using socket object sk, The server side uses channels conn """ info = str(sk.recv(1024), encoding='utf8') print(info) # Close the connection # The client is closed for use socket object sk, The server side uses channels conn sk.close()