------ similar cmd function, client Run, Server commanding results to client
-----------server.py-------------------
The subprocess Import
# subprocess.Popen, primarily Popen This class implements the shell
Import Socket
SK = socket.socket ()
Print (SK)
address = ( '127.0.0.1', 8003)
sk.bind (address)
sk.listen (. 3)
Print ( 'Waiting .....')
the while True:
Conn, addr = sk.accept ()
Print (addr)
the while True:
the try:
Data = conn.recv (1024)
the except Exception:
BREAK
IF Not Data: BREAK
Print ( '.....', STR (Data, 'UTF8'))
obj = subprocess.Popen (STR (Data, 'UTF8'), the shell = True, subprocess.PIPE = stdout)
# = stdout represented subprocess.PIPE through the pipeline by the PIP process your child go to my main process and package the object obj in
---- that is the function of this line and this line is theclient Results solution out instruction sent
= obj.stdout.read cmd_result ()
result_len bytes = (STR (len (cmd_result)), 'UTF8') # calculate its size
conn.sendall (result_len) # size of this figure passed Client
conn.recv (1024 )
conn.sendall (bytes (cmd_result))
sk.close ()
----------------------------- client.py --- ----------------------------
import socket
sk=socket.socket()
print(sk)
address=('127.0.0.1',8003)
sk.connect(address)
while True:
inp=input('>>>')
if inp == 'exit':
break
sk.send(bytes(inp,'utf8'))
result_len=int(str(sk.recv(1024),'utf8'))
sk.sendall(bytes('111','utf8'))
print(result_len)
data=bytes()
while len(data) != result_len:
recv= sk.recv(1024)
data+=recv
print(str(data,'gbk'))
sk.close()
The results as shown below: In the client performing dir and ipconfig commands to get results