Python 3.x Python,捕获操作系统输出并在discord中作为消息发送
对于我制作的机器人,我希望能够查看运行它的pi的温度(当然,该命令只能由开发人员使用)。我的问题是我无法获得终端命令的输出。我知道命令半工作,因为我可以在pi的屏幕上看到正确的输出,但是bot只向聊天发布一个“0” 我尝试过的事情:Python 3.x Python,捕获操作系统输出并在discord中作为消息发送,python-3.x,subprocess,python-asyncio,Python 3.x,Subprocess,Python Asyncio,对于我制作的机器人,我希望能够查看运行它的pi的温度(当然,该命令只能由开发人员使用)。我的问题是我无法获得终端命令的输出。我知道命令半工作,因为我可以在pi的屏幕上看到正确的输出,但是bot只向聊天发布一个“0” 我尝试过的事情: async def cmd_temp(self, channel): proc = subprocess.Popen('/opt/vc/bin/vcgencmd measure_temp', stdou
async def cmd_temp(self, channel):
proc = subprocess.Popen('/opt/vc/bin/vcgencmd measure_temp',
stdout=subprocess.PIPE)
temperature = proc.stdout.read()
await self.safe_send_message(channel, temperature)
async def cmd_temp(self, channel):
await self.safe_send_message(channel,
(os.system("/opt/vc/bin/vcgencmd measure_temp")))
async def cmd_temp(self, channel):
temperature = os.system("/opt/vc/bin/vcgencmd measure_temp")
await self.safe_send_message(channel, temperature)
每一个都做同样的事情,在聊天中发布一个0,并在pi的屏幕上输出。如果有人能提供帮助,我将不胜感激。该模块允许您以异步方式处理子流程:
async def cmd_temp(self, channel):
process = await asyncio.create_subprocess_exec(
'/opt/vc/bin/vcgencmd',
'measure_temp',
stdout=subprocess.PIPE)
stdout, stderr = await process.communicate()
temperature = stdout.decode().strip()
await self.safe_send_message(channel, temperature)
请参阅中的更多示例