Skip to content

Instantly share code, notes, and snippets.

@cx0der
Created August 13, 2018 14:12
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 cx0der/cc8cc618f2ea2e9b49e60f9968324318 to your computer and use it in GitHub Desktop.
Save cx0der/cc8cc618f2ea2e9b49e60f9968324318 to your computer and use it in GitHub Desktop.
Pychain HTTP server
class HttpHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/blocks':
self.set_response_headers(200)
self.wfile.write(bytes(json.dumps(chain, default=jdefault), "utf8"))
elif self.path == '/peers':
self.set_response_headers(200)
self.wfile.write(bytes(json.dumps(peers), "utf8"))
else:
self.set_response_headers(400, "plain/text")
message = "Bad request"
self.wfile.write(bytes(message, "utf8"))
return
def do_POST(self):
length = int(self.headers['Content-Length'])
post_body = json.loads(self.rfile.read(length))
new_block = add_block(post_body["data"])
send_broadcast(new_block)
self.set_response_headers(201)
self.wfile.write(bytes(json.dumps(new_block, default=jdefault), "utf-8"))
def set_response_headers(self, code, content_type="application/json"):
self.send_response(code)
self.send_header("Content-type", content_type)
self.end_headers()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment