# socket """ 1、Socket also called " Socket ", Applications usually go through " Socket " Send a request to the network or answer a network request , To enable communication between hosts or processes on a computer . 2、server End : Server side , Listen on the specified port , Provide services 3、client End : client , Connect server , Access to services """
Server side
# Refer to the module import socket # initialization ( Instantiation )socket object sk sk = socket.socket() # Define the binding IP port , Tuple format address = ('127.0.0.1', 8088) # binding IP port sk.bind(address) # Set the maximum number of connections sk.listen(5) # Initialize connection ,conn Get access to ,addr Get remote address , Has reached the awaited """ 1、 After initializing the connection, it will enter the blocking state , Waiting for the connection 2、 After the client is connected, it can send and receive information through the channel """ # Print channel and remote address information conn, addr = sk.accept() print(conn) print(addr) # 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 """ # 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(conn.recv(1024), encoding='utf8') print(info) # 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 """ conn.sendall(bytes('hello too', encoding='utf8')) # Close the connection # The client is closed for use socket object sk, The server side uses channels conn conn.close()
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()