ImportSocket#From socket Import *Ip_port=('127.0.0.1', 8000) Back_log=5buffer_size=1024Tcp_server=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) Tcp_server.bind (ip_port) Tcp_server.listen (back_log)Print('start running on the service side! ') conn,addr=tcp_server.accept ()Print('Two-way connection:', conn)Print('Client Address:', addr) whileTrue:data=conn.recv (buffer_size)Print('messages from the client:', Data.decode ('Utf-8')) msg=input ('service-side input >>>:') Conn.send (Msg.encode ('Utf-8')) Conn.close () tcp_server.close ()
Service Side
ImportSocket#From socket Import *Ip_port= ('127.0.0.1', 8000) Buffer_size=1024tcp_client=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) Tcp_client.connect (ip_port) whiletrue:msg=input ('client input >>>:'). Strip () Tcp_client.send (Msg.encode ('Utf-8')) Print('The client has sent a message!') Data=tcp_client.recv (buffer_size)Print('information from the service side:', Data.decode ('Utf-8') ) Tcp_client.close ()
Client
python-using the socket client server to loop and send messages