Commit 29e71a36 authored by Peter-Bernd Otte's avatar Peter-Bernd Otte

Added Unicode support

parent 8f744426
......@@ -7,6 +7,7 @@ import os
import parse
from http.server import HTTPServer, BaseHTTPRequestHandler
import logging, argparse
from urllib.parse import unquote # for converting url encoded string to unicode string
logging.basicConfig(format="%(asctime)-15s %(levelname)-8s %(message)s")
logger = logging.getLogger("MQTT-HTTP-Server")
......@@ -66,22 +67,25 @@ logger.info ("MQTT Loop started.")
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/json")
self.send_header("Content-type", "text/json;charset=UTF-8")
self.send_header("Access-Control-Allow-Origin", "<origin> | *")
self.end_headers()
# self.path equals GET Parameter: "/foo/bar/".
erg = parse.parse("/{topic}/set?{value:d}", self.path)
if not erg: erg = parse.parse("/{topic}", self.path)
#print(erg)
if erg and erg['topic'] in mlast:
topic = unquote(erg['topic']) if erg else None
if erg and topic in mlast:
print("topic:",topic)
if 'value' in erg:
logger.info("publish: "+str(erg['topic'])+" value: "+str(erg['value']) )
client.publish(erg['topic']+"/set", erg['value'], qos=1, retain=False) # send command to Relais
self.wfile.write(str.encode('{"result":"sucess", "topic":"'+erg['topic']+'", "value":'+str(erg['value'])+'}'))
logger.info("publish: "+str(topic)+" value: "+str(erg['value']) )
client.publish(topic+"/set", erg['value'], qos=1, retain=False) # send command to Relais
self.wfile.write(str.encode('{"result":"sucess", "topic":"'+topic+'", "value":'+str(erg['value'])+'}'))
else:
self.wfile.write(str.encode("/*You accessed path: {}*/\n{}".format(erg['topic'],
json.dumps(mlast[erg['topic']]) )) )
self.wfile.write(str.encode("/*You accessed path: {}*/\n{}".format(topic,
json.dumps(mlast[topic]) )) )
else:
self.wfile.write(str.encode(json.dumps(mlast) ))
self.wfile.write(json.dumps(mlast, ensure_ascii=False).encode('utf8') )
try:
httpd = HTTPServer((args.http_hostname, args.http_port), SimpleHTTPRequestHandler)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment