[英]Execute linux command with Python CGI
我正在嘗試制作一個Python CGI腳本來控制我的Raspberry PI開發板,我想打開/關閉網頁上的LED,但是該腳本不起作用,這是trial.py的代碼:
#!/usr/bin/python
import subprocess
import cgi
print("Content-Type: text/html\n\n")
print('<html><body>')
print('<p>Hello World!</p>')
print('</body></html>')
subprocess.call(["/bin/gpio -g write 23 1"])
我將針腳23從外殼設置為出模式
如果我使用python trial.py運行它,則會收到以下錯誤
Traceback (most recent call last):
File "trial.py", line 9, in <module>
subprocess.call(["/bin/gpio -g write 23 1"])
File "/usr/lib/python3.4/subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.4/subprocess.py", line 858, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.4/subprocess.py", line 1456, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: '/bin/gpio -g write 23 1
而如果我從localhost / cgi-bin / trial.py運行它,它將打印Hello World且沒有錯誤
任何想法 ?
感謝你
對於樹莓派,您可以使用幾種方法來打開和關閉GPIO引腳。 據我所知,這不是其中之一。
一種簡單的方法是只調用一次以下代碼。
open("/sys/class/gpio/export", "w").write("23")
open("/sys/class/gpio/gpio23/direction", "w").write("out")
這將導出並設置gpio引腳為輸出模式。 然后,調用以下代碼以打開該引腳。
open("/sys/class/gpio/gpio23/value", "w").write("1")
如果使用0而不是1,則該引腳將關閉。
或者,您可以使用RPI.GPIO ,其內容類似於以下代碼。
import RPi.GPIO as GPIO
# You need to use pin 16, as RPi.GPIO maps to the physical locations of the pins,
# not their BCM names.
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, 1) # Or 0
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.