Python,捕获 OS 输出并在不和谐中作为消息发送
Python, capture OS output and send as a message in discord
对于我制作的机器人,我希望能够查看 pi 运行 的温度(当然该命令只能由开发人员使用)。我的问题是我无法接缝获取终端命令的输出。我知道这个命令只起作用了一半,因为我可以在 pi 的屏幕上看到正确的输出,但机器人只在聊天中发布了一个“0”。
我尝试过的事情:
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 的屏幕上输出。如果有人能提供帮助,我将不胜感激
asyncio.subprocess 模块允许您以异步方式处理子流程:
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)
在 asyncio user documentation 中查看更多示例。
对于我制作的机器人,我希望能够查看 pi 运行 的温度(当然该命令只能由开发人员使用)。我的问题是我无法接缝获取终端命令的输出。我知道这个命令只起作用了一半,因为我可以在 pi 的屏幕上看到正确的输出,但机器人只在聊天中发布了一个“0”。
我尝试过的事情:
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 的屏幕上输出。如果有人能提供帮助,我将不胜感激
asyncio.subprocess 模块允许您以异步方式处理子流程:
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)
在 asyncio user documentation 中查看更多示例。