Implementation ideas:
1. Provide the client with input instructions;
2. Provide the service side that returns the result of the execution instruction
3. Find out the solution of the return result of the server one cannot receive all
Service-side code (ssh_server.py)
1 #Coding=utf-82 ImportSocket,os3 4Server =Socket.socket ()5Server.bind (('localhost', 9999))6 Server.listen ()7CLIENT,ADDR =server.accept ()8 whileTrue:9data = CLIENT.RECV (1024)Ten ifdata = ="' ordata = = None:Continue OneCmd_res =Os.popen (Data.decode ()). Read () A ifLen (cmd_res) = =0: -Cmd_res ='The instruction is invalid, please re-enter' -Client.send (str (LEN (Cmd_res.encode ('Utf-8')). Encode ('Utf-8')) theCLIENT.RECV (1024)#after the change -Client.send (Cmd_res.encode ('Utf-8')) - Print(Len (cmd_res)) -Server.close ()
Client code (ssh_client.py)
1 #coding = Utf-82 ImportSocket3 4Client =Socket.socket ()5Client.connect (('localhost', 9999))6 whileTrue:7cmd = input ('>>:'). Strip ()8 ifLen (cmd) = = 0:Continue9Client.send (Cmd.encode ('Utf-8'))Tendata_size = Int (CLIENT.RECV (1024). Decode ()) One Print(data_size)#byte size ACurr_size =0 -Final_res = b"' - whileData_size! =curr_size: thedata = CLIENT.RECV (1024) -Curr_size + =len (data) - Print(curr_size) -final_res+=Data + Print(Final_res.decode ())
Potential problems :
Because the server two times to send data intermittent time is too short, easy to cause sticky packets (the server sends out two messages at the same time to the cache, the buffer is not full or no timeout internal mechanism is to know that the buffer is full to take out the message, many times the information data sent due to this mechanism constraints
Easy to cause sticky bag, solve the idea of a. Server two send information the middle plus one is the time Gap, Time.sleep (2); b. The server returns an identity after the message has been received by the client, and a second wave message is sent after the service has accepted the identity.
Improved post Code (SSH_SERVER.PY):
#coding =utf-8
Import Socket,os
Server = Socket.socket ()
Server.bind ((' localhost ', 9999))
Server.listen ()
CLIENT,ADDR = Server.accept ()
While True:
data = CLIENT.RECV (1024)
if data = = "or data = = None:continue
Cmd_res = Os.popen (Data.decode ()). Read ()
If Len (cmd_res) = = 0:
Cmd_res = ' instruction is invalid, please re-enter '
Client.send (str (cmd_res.encode (' Utf-8 ')). Encode (' Utf-8 '))
CLIENT.RECV (1024x768) #b方式更改后
Client.send (Cmd_res.encode (' Utf-8 '))
Print (len (cmd_res))
Server.close ()
Improved post code (ssh_client.py)
# coding = Utf-8
Import socket
Client = Socket.socket ()
Client.connect ((' localhost ', 9999))
While True:
cmd = input (' >>: '). Strip ()
If Len (cmd) = = 0:continue
Client.send (Cmd.encode (' Utf-8 '))
data_size = Int (CLIENT.RECV (1024x768). Decode ())
Client.send ("received the message ...") #b方式更改后
Print (data_size) #字节大小
Curr_size = 0
Final_res = B '
While data_size! = curr_size:
data = CLIENT.RECV (1024)
Curr_size + = len (data)
Print (curr_size)
Final_res+=data
Print (Final_res.decode ())
Socket programming simulation of SSH code implementation under Linux