Python 是否可以在每次apache请求后打开和关闭led
在有人打开我的网站后,我现在试着打开led灯两周。顺序应该是这样的:Python 是否可以在每次apache请求后打开和关闭led,python,apache,raspberry-pi,mod-wsgi,raspberry-pi4,Python,Apache,Raspberry Pi,Mod Wsgi,Raspberry Pi4,在有人打开我的网站后,我现在试着打开led灯两周。顺序应该是这样的: 收到的请求 Led亮起,0.5秒后再次熄灭 回复是发回的 我有一个树莓皮打开我的led 到目前为止,我的python代码: import time import RPi.GPIO as GPIO pin = 4 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.HIGH) time.
import time
import RPi.GPIO as GPIO
pin = 4
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(pin, GPIO.LOW)
它有时起作用,但现在网站显示出来了。
我试过使用WSGI,但有点复杂。
也许有人也有同样的想法,这对他很有用,他可以帮助我。你根本不需要任何Apache或任何复杂的东西,你只需要在Python提供的简单web服务器类中添加一些位来打开和关闭LED 只需调整此代码顶部的IP地址和端口,并在终端中运行它,然后连接到它所服务的地址:
#!/usr/bin/env python3
#import RPi.GPIO as GPIO
import os
from time import sleep
from http.server import BaseHTTPRequestHandler, HTTPServer
host_name = '192.168.0.8'
host_port = 63000
pin = 4
class MyServer(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
html = '''
<html>
<body style="width:960px; margin: 20px auto;">
<h1>Welcome to my Raspberry Pi</h1>
</body>
</html>
'''
# You may want to move these 3 setup lines to the very start of the program
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
sleep(0.1)
GPIO.output(pin, GPIO.LOW)
self.do_HEAD()
self.wfile.write(html.encode("utf-8"))
if __name__ == '__main__':
http_server = HTTPServer((host_name, host_port), MyServer)
print("Server Starts - %s:%s" % (host_name, host_port))
try:
http_server.serve_forever()
except KeyboardInterrupt:
http_server.server_close()
#/usr/bin/env蟒蛇3
#将RPi.GPIO导入为GPIO
导入操作系统
从时间上导入睡眠
从http.server导入BaseHTTPRequestHandler,HTTPServer
主机名='192.168.0.8'
主机端口=63000
引脚=4
类MyServer(BaseHTTPRequestHandler):
def do_头(自身):
自我发送_响应(200)
self.send_标题('Content-type','text/html')
self.end_头()
def do_获得(自我):
html=“”
欢迎来到我的树莓皮
'''
#您可能希望将这3个设置行移动到程序的最开始处
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(引脚,GPIO.OUT)
GPIO.输出(引脚,GPIO.高)
睡眠(0.1)
GPIO.output(引脚,GPIO.LOW)
self.do_HEAD()
self.wfile.write(html.encode(“utf-8”))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
http\u server=HTTPServer((主机名,主机端口),MyServer)
打印(“服务器启动-%s:%s”%(主机名,主机端口))
尝试:
http_server.serve_forever()
除键盘中断外:
http_server.server_close()
您根本不需要任何Apache,也不需要任何复杂的东西,只需在Python提供的简单web服务器类中添加用于打开和关闭LED的位即可
只需调整此代码顶部的IP地址和端口,并在终端中运行它,然后连接到它所服务的地址:
#!/usr/bin/env python3
#import RPi.GPIO as GPIO
import os
from time import sleep
from http.server import BaseHTTPRequestHandler, HTTPServer
host_name = '192.168.0.8'
host_port = 63000
pin = 4
class MyServer(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
html = '''
<html>
<body style="width:960px; margin: 20px auto;">
<h1>Welcome to my Raspberry Pi</h1>
</body>
</html>
'''
# You may want to move these 3 setup lines to the very start of the program
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
sleep(0.1)
GPIO.output(pin, GPIO.LOW)
self.do_HEAD()
self.wfile.write(html.encode("utf-8"))
if __name__ == '__main__':
http_server = HTTPServer((host_name, host_port), MyServer)
print("Server Starts - %s:%s" % (host_name, host_port))
try:
http_server.serve_forever()
except KeyboardInterrupt:
http_server.server_close()
#/usr/bin/env蟒蛇3
#将RPi.GPIO导入为GPIO
导入操作系统
从时间上导入睡眠
从http.server导入BaseHTTPRequestHandler,HTTPServer
主机名='192.168.0.8'
主机端口=63000
引脚=4
类MyServer(BaseHTTPRequestHandler):
def do_头(自身):
自我发送_响应(200)
self.send_标题('Content-type','text/html')
self.end_头()
def do_获得(自我):
html=“”
欢迎来到我的树莓皮
'''
#您可能希望将这3个设置行移动到程序的最开始处
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(引脚,GPIO.OUT)
GPIO.输出(引脚,GPIO.高)
睡眠(0.1)
GPIO.output(引脚,GPIO.LOW)
self.do_HEAD()
self.wfile.write(html.encode(“utf-8”))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
http\u server=HTTPServer((主机名,主机端口),MyServer)
打印(“服务器启动-%s:%s”%(主机名,主机端口))
尝试:
http_server.serve_forever()
除键盘中断外:
http_server.server_close()
您需要使用“钩子”将Python代码与apache集成。mod_wsgi
模块允许创建这样的钩子,例如,请参阅教程@EvertW您知道如何使用wsgi创建这样的钩子吗?因为您的教程会做出响应,但我不希望python做出响应,但我的Web服务器apache应该使用请求的文件进行响应您需要使用“钩子”将Python代码与apache集成。mod_wsgi
模块允许制作此类挂钩,例如,请参阅教程@EvertW,您知道如何使用wsgi创建这样一个钩子吗?因为您的教程会做出响应,但我不希望python做出响应,但我的Web服务器apache应该使用请求的文件进行响应。这是一个好主意,但我已经有一个完全配置的apache服务器,只想在apache之前运行一点python发回响应这是个好主意,但我已经有一个完全配置好的apache服务器,只想在apache发回响应之前运行一点python