diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0cfb762 --- /dev/null +++ b/.gitignore @@ -0,0 +1,130 @@ +# Byte-compiled / optimized / DLL files +.idea/* +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ diff --git a/bookapp.py b/bookapp.py index d2284c6..eed05a5 100644 --- a/bookapp.py +++ b/bookapp.py @@ -1,4 +1,4 @@ -import re +import traceback from bookdb import BookDB @@ -6,18 +6,69 @@ 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 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 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/wsgi_1.py b/wsgi_1.py old mode 100644 new mode 100755 index 85498d1..051017d --- a/wsgi_1.py +++ b/wsgi_1.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import datetime default = "No Value Set" @@ -21,11 +21,11 @@ def application(environ, start_response): 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().strftime('%B'), + date=datetime.datetime.now().day, + year=datetime.datetime.now().year, + client_ip=environ.get('REMOTE_ADDR', default) ) status = '200 OK'