i struggling weird issue. have python script accessed page via php , returns json object 20 variables. of them work of time except 1 returns empty value. directly running python script on server returns value every time, php never sees it. have tried output string, int, , string combined preset value. posted code has 2 shown, of functional values omitted length. ("cpul" 1 not working.)
pythonscript.py:
#!/usr/bin/python2.7 import os, json def getcputemperature(): res = os.popen('vcgencmd measure_temp').readline() tmp = (1.8 * float(res.replace("temp=","").replace("'c\n",""))) + 32 return(tmp) # return % of cpu used user character string def getcpuuse(): val = str(os.popen("top -n1 | awk '/cpu\(s\):/ {print $2}'").readline().strip(\ )[2:4]) return(val) result = {'cput': cpu_temp, 'cpul': cpu_usage} print json.dumps(result)
output ssh terminal: {"cpul": "9", "cput": 106.16000000000001}
phpscript.php passes result on browser:
<?php session_start(); try { $result = exec('/usr/bin/python /scripts/pyhtonscript.py'); echo $result; } catch (exception $e) { echo '{"res" : "error", "msg" : "caught exception: ' . $e->getmessage() . '"}'; } ?>
output browser: {"cpul": "", "cput": 106.16000000000001}
if change pythonscript.py 'result' say:
result = {'cput': cpu_temp, 'cpul': 'foo'}
output browser: {"cpul": "foo", "cput": 106.16000000000001}
and if change pythonscript.py 'result' say:
result = {'cput': cpu_temp, 'cpul': 'foo' + cpu_usage}
output browser: {"cpul": "foo", "cput": 106.16000000000001}
if modify function output int rather string same results without quotes:
output ssh terminal: {"cpul": 9, "cput": 106.16000000000001}
output browser: {"cpul": "", "cput": 106.16000000000001}
the value percentage, love multiply 100 before sending, if modify function as:
def getcpuuse(): val = str(os.popen("top -n1 | awk '/cpu\(s\):/ {print $2}'").readline().strip(\ )) mod = int(val) * 100 return(mod)
output ssh terminal: {"cpul": 90, "cput": 106.16000000000001}
output browser: nothing, blank screen
apache2/error.log:
traceback (most recent call last): file "/var/www/html/assets/scripts/info_lkp.py", line 49, in <module> cpu_usage = getcpuuse() file "/var/www/html/assets/scripts/info_lkp.py", line 29, in getcpuuse mod2 = int(mod)*10 valueerror: invalid literal int() base 10: '' 'unknown': need more specific.
any idea missing before run out of hair pull out? stated, posted code truncated remove unrelated working similar functions , associated outputs.
this python error
when multiply 100 before sending it, in python script, error happens there.
and value being lost, well, not because of php. it's because python script not return valid json.
read error here valueerror: invalid literal int() base 10: ''
Comments
Post a Comment