From 8dabe401070f3cbcb557c2fb53327875e3fec039 Mon Sep 17 00:00:00 2001 From: geekwriter2 Date: Fri, 18 Dec 2020 09:30:54 -0800 Subject: [PATCH] michael_mcdonald submitting lesson4 ex, wscgi --- .idea/.gitignore | 3 + .idea/inspectionProfiles/Project_Default.xml | 25 ++++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ .idea/wsgi.iml | 15 +++++ __pycache__/bookdb.cpython-38.pyc | Bin 0 -> 1649 bytes bookapp.py | 60 ++++++++++++++++-- bookdb.py | 2 + wsgi_1.py | 14 ++-- 11 files changed, 127 insertions(+), 16 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/wsgi.iml create mode 100644 __pycache__/bookdb.cpython-38.pyc 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 0000000000000000000000000000000000000000..ef77d015b9b3361917b328d8645000078b327392 GIT binary patch literal 1649 zcma)6&2HO95GE;_mL1!PlQgwY-5d%;fF+QU{3F*jEK5m@pmv1Vh5z(Ir)zp z5MrEaHw2<8&=zl7=NItJH^y;K@M6jJ8&bTc zLEIXtE^BGDq+J+{CyRR_Q*j`o!M;ve*&rO#GLpwrW(sLus<0P|0EcQHlpFh;CFj6; zV)yA->2wthI}>N>kmffPSum1IAhfm8C&P41=PxIdEPoax(711w)^#!|_u5;teg*aX zXaQm7$xZZ3m&Tyq0gIHHe+8Z_ruq1ccZ9Z4X$&)2y&XAydy;b|S>NY4qP{;_^!-SX zVW{;b-+wd2;q1-4?-LRDehNvh=(xHTw?qn9_mPgJG_6_Fwz7`Yza`(+dQ{4BV~n0E zMS0AC)?M@p4;Vqya-j>_M%BR}WC4zu;HXY7X($E}SSKr$?VY?=amzcNw^QCp9yf5% zk5OaThQjn1@1iHiSOz^*@-|S>b+>cL!_#qii5W*nGz@814E6h*owixiW~$Ab3xlaY zl}!f^p=V@rI7_A@BsvwHc!VW|Ocd+#U34TyC>H1p0`}2&tWOa}HE5z?Ou;%}8kd?S z>uwgk?c!!-YctuZ;ixSc=~8sg*(EeCi^r%TsZ!q{4^hHWyfXyeR*ImKOYT-7agGiP zs4hmFj+5LoG2}29z>Y$qb3@z-jYmTE;YQJwIEsLd8DZs;3sc~hc9O5FgK50tKk@1` zpivuAFQ%k^Rb3W04=D4%d1X6a zN|s-~qAU!@=ooO}Bwr2bTLiUa 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

', '
    '] + item_template = '
  • {title}
  • ' + for book in all_books: + body.append(item_template.format(**book)) + body.append('
') + 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')]