Skip to content

Instantly share code, notes, and snippets.

@patrickcmserrano
Forked from souenzzo/lambdahttp.py
Created August 9, 2017 02:52
Show Gist options
  • Select an option

  • Save patrickcmserrano/2f0ee2b08e2e6ece32c7bde66ed0a358 to your computer and use it in GitHub Desktop.

Select an option

Save patrickcmserrano/2f0ee2b08e2e6ece32c7bde66ed0a358 to your computer and use it in GitHub Desktop.
import http.server
def bar(props):
return ["bar", {}, str(props)]
x = ["div", {},
["sp", {"onClick": "foobar()"}, ["code", {}, "foo"]],
[bar, 44, 666, 77],
["p", {}, "Olá mundo!"]]
def parseMeta(x):
s = ""
for i in x:
s = s + " " + i + "=\"" + x[i] + "\""
return s
def parse(x):
if type(x) == type([]):
fn, *args = x
if type(fn) == type(""):
tag, props, *body = x
metaStr = parseMeta(props)
bodyStr = ""
for i in body:
bodyStr = bodyStr + parse(i)
return "".join(["<", tag, metaStr, ">", bodyStr, "</", tag, ">"])
return parse(fn(args))
return str(x)
def handler(servlet_request, client_address, servlet):
f = servlet_request.makefile("wb")
f.write(b"HTTP/1.1 200 OK\r\n")
f.write(b"Content-Type: text/html\r\n")
f.write(b"\r\n")
f.write(parse(x).encode())
addr = ("", 8000)
srv = http.server.HTTPServer(addr, handler)
srv.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment