Created
August 13, 2018 14:12
-
-
Save cx0der/cc8cc618f2ea2e9b49e60f9968324318 to your computer and use it in GitHub Desktop.
Pychain HTTP server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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