标签:lis recv ddr proc 空值 connect process bsp div
# -*- coding: utf-8 -*- from socket import * import subprocess server = socket() server.bind((‘‘,10000)) server.listen(5) while True: conn,addr = server.accept() print( ‘客户端ip地址:‘,addr ) while True: try: cmd = conn.recv(1024) # if not cmd: break # 如果是 print( ‘收到命令:‘,cmd ) # 生成可执行命令 res = subprocess.Popen(cmd.decode(‘gbk‘),shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE) err = res.stderr.read() # 执行命令 if not err: # 命令正确 cmd_res = err # 返回执行结果 else: cmd_res = res.stderr.read() # 错误命令,再读进是空值 conn.send(cmd_res) # 结果返回客户端 except Exception as e: print(‘111‘,e) break conn.close()
# -*- coding: utf-8 -*- from socket import * cl = socket() cl.connect((‘127.0.0.1‘,10000)) while True: cmd = input(‘--->>>‘) if not cmd: continue if cmd == ‘quit‘: break cl.send(cmd.encode(‘gbk‘)) re = cl.recv(2048) print(‘命令执行结果:‘,re.decode(‘gbk‘))
标签:lis recv ddr proc 空值 connect process bsp div
原文地址:https://www.cnblogs.com/gdwz922/p/9311762.html