Skip to content

Instantly share code, notes, and snippets.

@CarlFK
Created October 21, 2019 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlFK/9b9d86e06e1f13fb53b6aa60e755ca4e to your computer and use it in GitHub Desktop.
Save CarlFK/9b9d86e06e1f13fb53b6aa60e755ca4e to your computer and use it in GitHub Desktop.
httm to mmqt bridge
import json
import os
from http.server import HTTPServer, BaseHTTPRequestHandler
from pprint import pprint
class web_server(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
data = json.loads(self.rfile.read(content_length))
publish.single(topic, data, server=mmqt_server)
magic wait for mmqt message
if mmqt ok:
self.send_response(200)
# return a pretty printed version back to whoever sent it.
# (in case someone wants to debug what we got.
# it's not a lot.)
ppjson = json.dumps(cng, indent=2)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(ppjson.encode('utf-8'))
else:
self.send_response(400)
def do_GET(self):
# some left over code that may be useful if we need to GET
# more than a hearbeat
if self.path == '/':
self.path = '/index.html'
try:
content = open(self.path[1:]).read()
self.send_response(200)
if os.path.splitext(self.path)[1]==".json":
self.send_header('Content-type', 'application/json')
except FileNotFoundError as e:
content = "The beating of our hearts is the only sound.\n"
self.send_response(404)
self.end_headers()
self.wfile.write(bytes(content, 'utf-8'))
httpd = HTTPServer(('localhost', 8087), web_server)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment