Last active
August 29, 2015 14:17
-
-
Save robleh/c3edb6629d539b994694 to your computer and use it in GitHub Desktop.
Hacker School Challenge
This file contains hidden or 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
| '''This is a Python based socket server which runs on localhost port 4000. | |
| Use /set?somekey=somekeyvalue to store a key pair. Use /get?key=somekey to | |
| retrieve a key's value.''' | |
| import socket | |
| import sys | |
| HOST = "" | |
| PORT = 4000 | |
| key_pairs = {} | |
| def create_socket(): | |
| #Initialize TCP connection | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| #Allow the port to be reused | |
| s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
| print "Socket created." | |
| #Bind socket to localhost and port | |
| try: | |
| s.bind((HOST, PORT)) | |
| except socket.error, msg: | |
| print "Bind failed. Error code :", str(msg[0]), "Message", msg[1] | |
| sys.exit() | |
| #Start listening on socket with handling for 1 clients | |
| s.listen(1) | |
| print "Waiting for connection..." | |
| return s | |
| def accept_connection(s): | |
| #Keep server alive and open for connections | |
| while True: | |
| conn, addr = s.accept() | |
| print "Database accessed from", addr[0] + ":", str(addr[1]) | |
| #Read data from HTTP request | |
| data = conn.recv(1024) | |
| #Separate the GET string from the url | |
| GET_string = data.split(" ")[1] | |
| #Check for values within the GET string | |
| if GET_string == "/": | |
| conn.send("Use /set? or /get? to interact with the server.") | |
| elif "set" in GET_string: | |
| key = set_key_pair(GET_string.lower()) | |
| conn.send("%s is now set to %s" % (key, key_pairs[key])) | |
| print "%s is now set to %s" % (key, key_pairs[key]) | |
| elif "get" in GET_string: | |
| key = get_key_pair(GET_string.lower()) | |
| conn.send("%s has a value of %s" % (key, key_pairs[key])) | |
| print "%s has a value of %s" % (key, key_pairs[key]) | |
| #Close current connection | |
| conn.close() | |
| def set_key_pair(url_string): | |
| #Split the key pair from the action variable | |
| key_pair = url_string.split("?")[1] | |
| #Split the key from the key value | |
| key = key_pair.split("=")[0] | |
| key_value = key_pair.split("=")[1] | |
| #Store the key pair globally | |
| key_pairs[key] = key_value | |
| return key | |
| def get_key_pair(url_string): | |
| #Split the key from the action variable | |
| key_chunk = url_string.split("?")[1] | |
| key = key_chunk.split("=")[1] | |
| return key.lower() | |
| '''Not sure how to trigger closing the socket cleanly.''' | |
| def close_socket(): | |
| s.close() | |
| print "Closing out." | |
| def main(): | |
| the_socket = create_socket() | |
| connection = accept_connection(the_socket) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment