Service side
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #_author_ = ' HJM '4 #@Time: 2018/1/20 pm 8:105 #@Email: [Email protected]6 #@File: server_socket.py7 #@Software: Pycharm8 ImportSocket,os9 TenServer=Socket.socket () OneServer.bind (('127.0.0.1', 9999)) A #Server-bound address - Server.listen () - #service-Side monitoring the whileTrue: -Con,addr=server.accept () - - Print("New Conn", addr) + whileTrue: - Print('wait for the new instructions! ') +DATA=CON.RECV (1024) A if notData: at Print('client has been disconnected') - Break -cmd_res=Os.popen (Data.decode ()). Read () - ifLen (cmd_res) = =0: -cmd_res='Out put ....' -Cmd_sazi=Len (cmd_res) inCon.send (str (cmd_sazi). Encode ('Utf-8')) - to #Con.recv (1024x768) #分割粘包 +Con.send (Cmd_res.encode ('Utf-8')) - Print("Send down") theServer.close ()
Server SocketClient
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #_author_ = ' HJM '4 #@Time: 2018/1/20 pm 8:105 #@Email: [Email protected]6 #@File: client_socket.py7 #@Software: Pycharm8 ImportSocket9 TenClinet =Socket.socket () OneIp_port = ('127.0.0.1', 9999) A Clinet.connect (Ip_port) - whileTrue: -cmd = input ('>>:'). Strip () the #client input command, LS, ifconfig - ifLen (cmd) = = 0:Continue #cannot send empty data -Clinet.send (Cmd.encode ('Utf-8')) - + #Client Send Instructions -Cmd_saiz = CLINET.RECV (1024) + #the size of data sent by the receiving service A Print(Cmd_saiz) at #Clinet.send (b ' 123 ') -Received_saiz =0 - - whileReceived_saiz! = Int (Cmd_saiz.decode ('Utf-8')):#determine whether the received size is consistent with the size of the send -data = CLINET.RECV (1024) - #client receives 1024 bytes of data inReceived_saiz + =len (data) - to Print(Data.decode ()) +Clinet.close ()
Client Socket
Python socket programming