我在Python中有一个Web服务器:
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from os import curdir, sep
import RPi.GPIO as GPIO
import time
PORT_NUMBER = 8080
def onoff(pin, on_off):
GPIO.output(pin, on_off)
return
class myHandler(BaseHTTPRequestHandler):
params = {}
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
if self.path.startswith("/favicon.ico"):
return
elif self.path=="/":
f = open(curdir + sep + "/index.html")
self.wfile.write(f.read())
f.close()
elif self.path.startswith("/remote.html"):
GPIO.setmode(GPIO.BOARD)
self.params = {}
if self.path.find("?") > -1:
p = self.path.split("?")[-1]
p = p.split("&")
for v in p:
pair = v.split("=")
self.params[pair[0]] = pair[1]
led = int(self.querystring("led"))
GPIO.setup(led, GPIO.OUT)
if self.querystring("action") == "on":
onoff(led, GPIO.HIGH)
elif self.querystring("action") == "off":
onoff(led, GPIO.LOW)
self.wfile.write("Led " + str(led))
return
def querystring(self, key):
return self.params[key]
try:
GPIO.cleanup()
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ' , PORT_NUMBER
server.serve_forever()
GPIO.cleanup()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
但我无法在index.html页面中为标记加载图片,我该怎么办?
答案 0 :(得分:1)
您可以添加另一个" elif":
# ...
elif self.path == "/yourimage.jpeg":
self.send_header('Content-type','image/jpeg')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
with open("yourimage.jpg", "rb") as fout:
self.wfile.write(fout.read())
# ...
移动这三行...
self.send_header('Content-type','text/html')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
这是由于 对于每个已经存在的,除了新的
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from os import curdir, sep
import RPi.GPIO as GPIO
import time
PORT_NUMBER = 8080
def onoff(pin, on_off):
GPIO.output(pin, on_off)
return
class myHandler(BaseHTTPRequestHandler):
params = {}
def do_GET(self):
self.send_response(200)
if self.path.startswith("/favicon.ico"):
return
elif self.path=="/":
self.send_header('Content-type','text/html')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
f = open(curdir + sep + "/index.html")
self.wfile.write(f.read())
f.close()
elif self.path.startswith("/remote.html"):
self.send_header('Content-type','text/html')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
GPIO.setmode(GPIO.BOARD)
self.params = {}
if self.path.find("?") > -1:
p = self.path.split("?")[-1]
p = p.split("&")
for v in p:
pair = v.split("=")
self.params[pair[0]] = pair[1]
led = int(self.querystring("led"))
GPIO.setup(led, GPIO.OUT)
if self.querystring("action") == "on":
onoff(led, GPIO.HIGH)
elif self.querystring("action") == "off":
onoff(led, GPIO.LOW)
self.wfile.write("Led " + str(led))
elif self.path == "/yourimage.jpeg":
self.send_header('Content-type','image/jpeg')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
with open("yourimage.jpg", "rb") as fout:
self.wfile.write(fout.read())
return
def querystring(self, key):
return self.params[key]
try:
GPIO.cleanup()
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ' , PORT_NUMBER
server.serve_forever()
GPIO.cleanup()
except KeyboardInterrupt:
print '^C received, shutting down the web server'