Sockets are also commonly referred to as "sockets," which describe IP addresses and ports, and are a handle to a communication chain that can be used to communicate between different virtual machines or different computers.
Socket Server Example:
ImportSocketip_port= ('127.0.0.1', 9999)#set the IP address and port on the server side. SK= Socket.socket ()#call the socket constructor. Sk.bind (Ip_port)#bind the server-side IP and port using the Bind method. It can also be understood to register IP and port information with the system. Sk.listen (5)#sets the maximum number of connections. whileTrue:Print('Server Waiting ...') conn,addr= Sk.accept ()#the server socket waits for the client to request a connection through the socket's accept method. Client_data = CONN.RECV (1024)#set up to accept up to 1024 bytes at a time Print(Str (Client_data,'UTF8'))#The print service side receives the client's information. Conn.sendall (Bytes ('Don't answer, don't answer , don't answer','UTF8'))#use the Sendall method to send messages to the client. Conn.close ()#Close the connection. Socket Server Example:
Socket Client Example:
ImportSocketip_port= ('127.0.0.1', 9999)#set the IP and open ports on the server side. SK= Socket.socket ()#calling the socket constructorSk.connect (Ip_port)#connect to the server using the socket's Connect methodsk.sendall (Bytes ('request to occupy the Earth','UTF8'))#use the Sendall method to send messages to the server. server_reply = SK.RECV (1024)#set up to accept up to 1024 bytes at a timePrint(Str (server_reply,'UTF8'))#Print out the received service-side back information. Sk.close ()#close the socket connection. Socket Client Example:
The path to learning in Python--socket