import os #导入os操作模块
# 系统目录间的分隔符,
# Linux: /var/log/messages
# Windows: C:\\Project\hello.py
import itchat
print(os.path.sep)
# 在Linux里面执行shell命令
# 1. 第一种方式: 可以判断命令是否执行成功; 返回值为0, 执行成功, 返回值不为0, 执行失败
res = os.system('hostnam1e')
print("res:", res)
# 2. 第二种方式: 用来保存命令的执行结果
res = os.popen('hostname').read()
print("res:", res)
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
# 需求: 当文件助手发送消息, 执行发送的内容,
# 1). 如果执行成功, 【显示执行成功】:执行结果
# 2). 反之, 显示之心失败 .
# print(msg)
if msg['ToUserName'] == 'filehelper':
# 获取要执行命令的内容
command = msg['Content']
# print(command)
# 让电脑执行命令代码
# 如果执行成功, 返回值为0
if os.system(command) == 0:
res = os.popen(command).read()
result = "【返回值】- 命令执行成功, 执行结果:\n" + res
itchat.send(result, 'filehelper')
# 命令执行失败, 请重新输入命令
else:
result = "【返回值】- 命令%s执行失败, 请重试" %(command)
itchat.send(result, 'filehelper')
if __name__ == "__main__":
itchat.auto_login(hotReload=True)
itchat.run()
执行结果如下: