Answer the question
In order to leave comments, you need to log in
Python subrocess wsgi. How to correctly read the value from the file?
Hello! The problem is this: It is
necessary to read the gpio value from the file.
The result of executing the command "cat /sys/devices/virtual/gpio/gpio27/value" is "0", as it should be
WSGI is connected to the Apache server, the script is written:
import os
import glob
import subprocess
result = subprocess.call("cat /sys/devices/virtual/gpio/gpio27/value", shell = True)
if result == 0:
state = "ON"
else:
state = "OFF"
def application(environ, start_response):
status = '200 OK'
output = state
response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Answer the question
In order to leave comments, you need to log in
RTFM carefully, gentlemen.
subprocess.call() returns an exit code. cat successfully reads the value from the file, that is, the exit code is always 0 (yes, exactly int).
And all because you initially chose a transrectal solution. Why call cat from a Python program when you can just open a file and read from it?
with open('/sys/devices/virtual/gpio/gpio27/value') as f:
result = f.read()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question