Skip to content

Instantly share code, notes, and snippets.

@xmunoz
Last active November 8, 2016 20:55
Show Gist options
  • Save xmunoz/bbd0c016b5c8c3c104bb78150eb22b93 to your computer and use it in GitHub Desktop.
Save xmunoz/bbd0c016b5c8c3c104bb78150eb22b93 to your computer and use it in GitHub Desktop.
recurse center pairing interview
from http.server import HTTPServer, BaseHTTPRequestHandler
import re
HOST, PORT = "localhost", 4000
INPUTS = {}
class HashHandler(BaseHTTPRequestHandler):
# assuming the challenge refers to HTTP GET requests
def do_GET(self):
regex = r"/(set|get)\?(\w+)=(\w+)"
result = re.fullmatch(regex, self.path)
if result is None:
self.send_error(500, message="Invalid Request")
return
if result.group(1) == "set":
INPUTS[result.group(2)] = result.group(3)
self.send_response(200)
self.flush_headers()
return
elif result.group(1) == "get":
if result.group(2) != "key":
self.send_error(500, message="Invalid Request")
return
if result.group(3) not in INPUTS:
self.send_error(500, message="Invalid Request")
return
self.send_response(200)
content = INPUTS[result.group(3)]
body = content.encode('UTF-8', 'replace')
self.send_header("Connection", "close")
self.send_header("Content-Type", "text/plain")
self.send_header('Content-Length', len(body))
self.end_headers()
self.wfile.write(body)
else:
raise Exception("Here be dragons")
def main():
server = HTTPServer((HOST, PORT), HashHandler)
server.serve_forever()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment