方法一:subprocess
import subprocess
def get_command_result(command):
try:
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode == 0:
return result.stdout
else:
return result.stderr
except Exception as e:
return str(e)
command = 'ipconfig'
result = get_command_result(command)
print(result)
方法二:
import os
# 获取当前路径
dir_path = os.path.dirname(os.path.realpath(__file__))
# 拼接路径
dir_path1 = os.path.join(dir_path, 'requestRecord.py')
print(dir_path1)
command = 'mitmdump -s '+ dir_path1
# 用于更改命令提示符的代码页。这里的 65001 是 Unicode 编码,表示使用 UTF-8 编码。
os.system('chcp 65001')
r = os.system(command)
print(r)