Created
October 22, 2018 21:42
-
-
Save maltfield/96930a163e77eb6e2d2bfd95fc2f75e8 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python2.7 | |
################################################################################ | |
# File: databaseServer.py | |
# Version: 0.1 | |
# Purpose: Implements the Recurse Center's "Database Server" program. | |
# Authors: Michael Altfield <[email protected]> | |
# Created: 2018-10-22 | |
# Updated: 2018-10-22 | |
################################################################################ | |
################################################################################ | |
# IMPORTS # | |
################################################################################ | |
import time | |
import BaseHTTPServer | |
import urlparse | |
################################################################################ | |
# SETTINGS # | |
################################################################################ | |
DEBUG = False | |
HOST_NAME = '' | |
PORT_NUMBER = 4000 | |
################################################################################ | |
# MAIN BODY # | |
################################################################################ | |
db = dict() | |
# modified from the documentations' example here: | |
# * https://wiki.python.org/moin/BaseHttpServer | |
# TODO: implement dict() storage to disk with Data::Dumper equivalent = pickle | |
# * https://stackoverflow.com/questions/2540567/is-there-a-python-equivalent-to-perls-datadumper | |
# * https://docs.python.org/3/library/pickle.html | |
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
def do_HEAD(s): | |
s.send_response(200) | |
s.send_header("Content-type", "text/html") | |
s.end_headers() | |
def do_GET(s): | |
"""Respond to a GET request.""" | |
# declare variables | |
body = '' | |
endpoint = urlparse.urlparse( s.path ).path[1:] | |
requestVars=urlparse.parse_qs( urlparse.urlparse(s.path).query ) | |
# what endpoint did the client query? | |
if endpoint == 'get': | |
# the user wants to retrieve a value from the db for the given key | |
try: | |
key = requestVars["key"][0] | |
value = db[key] | |
body = value | |
except Exception, e: | |
s.send_response( 500 ) | |
s.send_header("Content-type", "text/html") | |
s.end_headers() | |
s.wfile.write( "Error: Unable to get variable (" + str(e) + ")" ) | |
return | |
elif endpoint == 'set': | |
# the user wants to set a key/value store to the db | |
# declare variables | |
key = list( requestVars.keys() )[0] | |
value = requestVars[key][0] | |
# store the key (and value) to the db | |
db[key] = value | |
body = 'Success' | |
else: | |
# we don't understand the endpoint that the user requestested; give error | |
s.send_response( 500 ) | |
s.send_header("Content-type", "text/html") | |
s.end_headers() | |
s.wfile.write( "Error: Unknown endpoint" ) | |
return | |
s.send_response(200) | |
s.send_header("Content-type", "text/html") | |
s.end_headers() | |
s.wfile.write("<html><head><title>RC Database Server (by Michael Altfield)</title></head>") | |
s.wfile.write("<body><p>" + body + "</p>") | |
s.wfile.write("</body></html>") | |
if __name__ == '__main__': | |
server_class = BaseHTTPServer.HTTPServer | |
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler) | |
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER) | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
httpd.server_close() | |
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER) | |
# exit cleanly | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment