V
V
vipvizor2016-01-07 10:24:31
Python
vipvizor, 2016-01-07 10:24:31

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]

The result of its execution is always "OFF", i.e. "one". Regardless of the contents of the file "value"
What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery Ryaboshapko, 2016-01-07
@vipvizor

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()

In this case, by the way, it will be a string unit.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question