diff --git a/calculator.py b/calculator.py index a46affd..8ccc4dc 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,5 @@ +import traceback + """ For your homework this week, you'll be creating a wsgi application of your own. @@ -42,14 +44,51 @@ """ +def home(): + return 'This site has four functions which add/subtract/multiply/devide two integers. They can be called out by adding the function followed by back slash a number another back slash and another number to the end of http://localhost:8080.' + + def add(*args): """ Returns a STRING with the sum of the arguments """ # TODO: Fill sum with the correct value, based on the # args provided. - sum = "0" + sum = int(args[0])+int(args[1]) + + return str(sum) + + +def subtract(*args): + """ Returns a STRING with the sum of the arguments """ + + # TODO: Fill sum with the correct value, based on the + # args provided. + subtract = int(args[0])-int(args[1]) + + return str(subtract) + + +def multiply(*args): + """ Returns a STRING with the sum of the arguments """ + + # TODO: Fill sum with the correct value, based on the + # args provided. + multiply = int(args[0])*int(args[1]) + + return str(multiply) + + +def divide(*args): + """ Returns a STRING with the sum of the arguments """ + + # TODO: Fill sum with the correct value, based on the + # args provided. + if args[1] == '0': + return "Undefined; attempting to divide by zero." + divide = int(args[0])/int(args[1]) + + return str(divide) - return sum # TODO: Add functions for handling more arithmetic operations. @@ -63,11 +102,24 @@ def resolve_path(path): # examples provide the correct *syntax*, but you should # determine the actual values of func and args using the # path. - func = add - args = ['25', '32'] + funcs = { + '': home, + 'add': add, + 'subtract': subtract, + 'multiply': multiply, + 'divide': divide, + } + path = path.strip('/').split('/') + func_name = path[0] + args = path[1:] + try: + func = funcs[func_name] + except KeyError: + raise NameError return func, args + def application(environ, start_response): # TODO: Your application code from the book database # work here as well! Remember that your application must @@ -76,9 +128,30 @@ def application(environ, start_response): # # TODO (bonus): Add error handling for a user attempting # to divide by zero. - pass + headers = [("Content-type", "text/html")] + try: + path = environ.get('PATH_INFO', None) + if path is None: + raise NameError + func, args = resolve_path(path) + body = func(*args) + status = "200 OK" + except NameError: + status = "404 Not Found" + body = "

Not Found

" + except Exception: + status = "500 Internal Server Error" + body = "

Internal Server Error

" + print(traceback.format_exc()) + finally: + headers.append(('Content-length', str(len(body)))) + start_response(status, headers) + return [body.encode('utf8')] + if __name__ == '__main__': # TODO: Insert the same boilerplate wsgiref simple # server creation that you used in the book database. - pass + from wsgiref.simple_server import make_server + srv = make_server('localhost', 8080, application) + srv.serve_forever()