python的新手,所以它可能很简单。Raspberry Pi 4,运行 apache 并连接了 CGI。Speedtest-cli 已安装并且工作正常。我可以在 Putty 中毫无问题地运行下面的脚本。但是当我尝试在 cgi-bin 中运行它进行测试时,我在这一行得到一个错误:
打印 ping.group(1)
错误:<type 'exceptions.AttributeError'>:'NoneType' 对象没有属性 'group'
它似乎跳过了调用 speedtest 的行: response = subprocess.Popen('/usr/bin/speedtest', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf- 8')
同样,这在 Putty 中运行良好。
#!/usr/bin/python
print ('Content-Type: text/html')
print
import os
import re
import subprocess
import time
import cgi, cgitb
cgitb.enable()
try:
while(True):
print("\rGetting data")
print("------------------------------------")
response = subprocess.Popen('/usr/bin/speedtest', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')
#print(response)
ping = re.search("Latency:\s+(.*?)\s+", response, re.MULTILINE)
download = re.search("Download:\s+(.*?)\s", response, re.MULTILINE)
upload = re.search("Upload:\s+(.*?)\s", response, re.MULTILINE)
ping = ping.group(1)
download = download.group(1)
upload = upload.group(1)
print("Ping: " + ping + " m/s")
print("Download: " + download + " mb/s")
print("Upload: " + upload + " mb/s")
time.sleep(1810)
print("\r\n\r\n")
except:
cgitb.handler()