subprocess模块
subprocess的补充
1.可以帮你通过代码执行操作系统的终端命令。
2.并且返回终端执行命令后的结果。
import subprocess
import subprocess
cmd = input('cmd==>:') #dir
obj = subprocess.Popen(
# cmd命令
cmd
#shell=True
shell=Ture
#返回正确结果参数
stdout = subprocess.PIPE
#返回错误结果参数。
stderr = subprocess.PIPE
)
res = obj.stdout.read() + obj.stderr.read()
print(res.decode('gbk'))
作业回顾 import subprocess
服务端
import socket
import subprocess
server = socket.socket()
server.bind('127.0.0.1',8888)
server.listen(5)
while True:
try:
#recv的数据是从内存中获取
cmd = conn.recv(1024).decode('utf_8')
if cmd =='q'
break
if len(cmd)==0:
continue
print(cmd)
#执行cmd命令
obj = subprocess.Popen(
# cmd命令
cmd
#shell=True
shell=Ture
#返回正确结果参数
stdout = subprocess.PIPE
#返回错误结果参数。
stderr = subprocess.PIPE
)
res = obj.stdout.read()+obj.stderr.read()
conn.send(result)
excep Exception as e:
print(e)
break
coon.close()
客户端:
import socket
client = socket.socket()
client.connect(
('127.0.0.1', 9527)
)
while True:
cmd = input('》》》:') # dir
client.send(cmd.encode('utf-8'))
if cmd == 'q':