cmd_server
Import Socket Import The subprocess # execute shell commands SK = socket.socket () address = ( ' 127.0.0.1 ' , 8000 ) sk.bind (address) sk.listen ( . 3 ) Print ( ' Waiting ... ' ) the while . 1 : Conn, addr = sk.accept () the while . 1 : the try : data = conn.recv (1024) # of data received incomplete too cause the except Exception AS E: Print (' The customer has left ' ) BREAK IF Not Data: BREAK Print (STR (Data, " UTF-. 8 ' )) obj = subprocess.Popen (STR (Data, " UTF-. 8 ' ), the shell = True, stdout = The subprocess. pIPE) # stdout standard output pIPE is the process transfer conduit act as the main process (the package) # if it can not be added to get a final assignment to the variable cmd_result = obj.stdout.read () result_len = bytes ( str (len (cmd_result)), ' UTF-. 8 ' ) # int type can not be directly converted to bytes required by an intermediate transition str conn.sendall (result_len) conn.recv ( 1024) # isolates the conn.sendall (cmd_result) sk.close () "" " " " Client then sends a command to the server side to return results to solve the problem: if a transmission of command result is too large to be received a plurality of times, the next command can not be executed resulting in solution: the results using the len function determines the length of the transmission, after transmission to the client terminal then the while loop is determined by the client Note: only 1 transmission will pass bytes class, while int can not be converted directly bytes so the need for excessive use str 2. send two together is likely to stick package, you need to be a cut off if a sleep time module time.sleep (), then the process will delay Therefore plus any 'closed aND dEVELOPMENT' like '' '
cmd_clinet:
import socket address=('127.0.0.1',8000) sk=socket.socket() sk.connect(address) while True: a=input(">>>") if a=='exit': break sk.send(bytes(a,'utf-8')) recv_len=int(str(sk.recv(1024),'utf-8')) sk.send('ok')#用于隔断 print(recv_len) data=bytes() while len(data)!=recv_len: recv=sk.recv(1024) data+=recv print(str(data,'gbk')) sk.close() """"" """