Based on study day 67 python tcp instantiate remote command execution

Use conduit for communication between two procedures: For example: QQ and micro-channel communication

Server:

Socket * Import from 
Import The subprocess # ducting module, communications among different programs
for ip_port = "127.0.0.1", 8080
The back_log. 5 =
buffer_size = 1024

tcp_server = Socket (AF_INET, SOCK_STREAM)
tcp_server.bind (for ip_port)
tcp_server.listen (. 5)
True the while:
conn, addr = tcp_server.accept ()
Print ( "new client link", addr)
the while True:
the try:
cnd = conn.recv (buffer_size)
IF not cnd: BREAK
Print ( "client received the command" , CND)

# run run give cnd_res
RES = subprocess.Popen (cnd.decode ( "UTF-. 8"), the shell = True, the information is encapsulated into # duct inside
stderr = subprocess.PIPE, # get the wrong information Official Road inside
subprocess.PIPE = stdin,
stdout = subprocess.PIPE)
ERR = res.stderr.read ()
IF ERR:
cnd_res ERR =
the else:
cnd_res = res.stdout.read () ## to read the correct information from the running inside Guandao out

# to run correctly good command to the client
conn.send (cnd_res)
the except Exception AS E:
Print (E)
BREAK
conn.Close ()

Client:

* Import socket from 
ip_port = "127.0.0.1", 8080
back_log = 5
buffer_size = 1024

tcp_clint = socket (AF_INET, SOCK_STREAM)
tcp_clint.connect (ip_port)
the while True:
cnd the INPUT = ( ">>>:") Strip (. )
IF not cnd: the Continue # can not be empty
if cnd == "quit": continue # press to exit, the exit is allowed
tcp_clint.send (cnd.encode ( "UTF-8"))
cnd_res = tcp_clint.recv (buffer_size)
Print ( "execution result of the command is:", cnd_res.decode ( "gbk" )) # server sends all over not specify the encoding using a coding system, Windows default encoding is GBK
tcp_clint.close ()

 

Guess you like

Origin www.cnblogs.com/jianchixuexu/p/11875431.html