我正在玩一个坐在我的Raspberry Pi上的Telegram机器人,一切正常,但我试图用命令/正常运行时间显示rpi的正常运行时间而没有成功,我尝试了:
elif command == '/uptime':
bot.sendMessage(chat_id, os.system("uptime"))
或
elif command == '/uptime':
uptime = os.system("uptime")
bot.sendMessage(chat_id,'%s' % uptime )
这是我最后一次尝试不成功的尝试:
elif command == '/uptime':
uptime = os.popen("awk '{print $1}' /proc/uptime").readline()
bot.sendMessage(chat_id, ('uptime(sec) = '+uptime))
错误在哪里?
以下是完整的代码:
import sys
import os
import telepot
import datetime
import time
from datetime import timedelta
id_a = [111111,2222222,3333333,4444444,5555555]
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
sender = msg['from']['id']
print 'Got command: %s' % command
if sender in id_a:
if command == '/ciao':
bot.sendMessage(chat_id, 'Hei, ciao!')
elif command == '/apri':
os.system("sudo python /home/pi/tg/xyz.py")
bot.sendMessage(chat_id, 'Ti ho aperto!')
elif command == '/uptime':
with open('/proc/uptime', 'r') as f:
usec = float(f.readline().split()[0])
usec_str = str(timedelta(seconds = usec))
bot.sendMessage(chat_id, '%s' % usec_str)
elif command == '/riavvia':
bot.sendMessage(chat_id, 'Rebooting...')
os.system("sudo reboot")
else:
bot.sendMessage(chat_id, 'Forbidden access!')
bot.sendMessage(chat_id, sender)
bot = telepot.Bot('myToken')
bot.message_loop(handle)
print 'I am listening ...'
while 1:
time.sleep(10)
答案 0 :(得分:1)
您需要从subprocess
获取输出。这是因为在其他情况下,Python会转换stdout
的第一个值。
获取正常运行时间的代码应如下所示:
import subprocess
direct_output = subprocess.check_output('uptime', shell=True)
这将为您提供direct_output
变量的正常运行时间。
答案 1 :(得分:0)
...试
<body>
<form>
// some text inputs
<?php echo makeList($parameter1, $parameter2); ?>
// submit button
</form>
<?php
// connect to database
function makeList($arg1, $arg2) {
echo '<select>';
while ($row = mysqli_fetch_array($result)){
echo "<option>";
echo $row[$column];
echo "</option>";
echo '</select>';
}
</body>