diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..d13b3da --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,25 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..2d83d70 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..7e38a11 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/wsgi.iml b/.idea/wsgi.iml new file mode 100644 index 0000000..4f2c9af --- /dev/null +++ b/.idea/wsgi.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/__pycache__/bookdb.cpython-38.pyc b/__pycache__/bookdb.cpython-38.pyc new file mode 100644 index 0000000..ef77d01 Binary files /dev/null and b/__pycache__/bookdb.cpython-38.pyc differ diff --git a/bookapp.py b/bookapp.py index d2284c6..7469af7 100644 --- a/bookapp.py +++ b/bookapp.py @@ -1,23 +1,69 @@ import re - +import traceback from bookdb import BookDB DB = BookDB() +def resolve_path(path): + """""" + funcs = {'': books, 'book': book,} + path = path.strip('/').split('/') + func_name = path[0] + args = path[1:] + try: + func = funcs[func_name] + except KeyError: + raise NameError + return func, args + + def book(book_id): - return "

a book with id %s

" % book_id + page = """ +

{title}

+ + + + +
Author{author}
Publisher{publisher}
ISBN{isbn}
+Back to the list +""" + book = DB.title_info(book_id) + if book is None: + raise NameError + return page.format(**book) def books(): - return "

a list of books

" + all_books = DB.titles() + body = ['

My Bookshelf

', '') + return '\n'.join(body) def application(environ, start_response): - status = "200 OK" - headers = [('Content-type', 'text/html')] - start_response(status, headers) - return ["

No Progress Yet

".encode('utf8')] + 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__': diff --git a/bookdb.py b/bookdb.py index f3a7241..9f49a9f 100644 --- a/bookdb.py +++ b/bookdb.py @@ -1,5 +1,7 @@ class BookDB(): + """books db""" + def titles(self): titles = [ dict(id=id, title=database[id]['title']) for id in database.keys() diff --git a/wsgi_1.py b/wsgi_1.py index 85498d1..4e24a48 100644 --- a/wsgi_1.py +++ b/wsgi_1.py @@ -1,6 +1,5 @@ #!/usr/bin/env python import datetime - default = "No Value Set" body = """ @@ -18,21 +17,18 @@ def application(environ, start_response): import pprint pprint.pprint(environ) - response_body = body.format( software=environ.get('SERVER_SOFTWARE', default), - path="aaaa", - month="bbbb", - date="cccc", - year="dddd", - client_ip="eeee" + path=environ.get('PATH_INFO', default), + month=datetime.datetime.now().month, + date=datetime.datetime.now().day, + year=datetime.datetime.now().year, + client_ip= environ.get('REMOTE_ADDR', default) ) status = '200 OK' - response_headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) - return [response_body.encode('utf8')]