Skip to content

finished activity for lesson04 #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions bookapp.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,74 @@
import re

import traceback
from bookdb import BookDB

DB = BookDB()


def book(book_id):
return "<h1>a book with id %s</h1>" % book_id
body = """<h1>{title}</h1>
<table>
<tr><th align='left'>Author:</th><td>{author}</td></tr>
<tr><th align='left'>Publisher:</th><td>{publisher}</td></tr>
<tr><th align='left'>ISBN:</th><td>{isbn}</td></tr>
</table>
<a href="/">Back to the list</a>
"""
the_book = DB.title_info(book_id)
if the_book is None:
raise NameError
return body.format(**the_book)


def books():
return "<h1>a list of books</h1>"
all_books = DB.titles()
body = ['<h1>My Bookself</h1>', '<ul>']
item_template = '<li><a href="/book/{id}">{title}</a></li>'
for book in all_books:
body.append(item_template.format(**book))
body.append('</ul>')
return '\n'.join(body)

def book_router(path):
functions = {
'': books,
'book': book
}
# strip the last '/', then split the path into words by
# "/"
# so localhost:8080/ should be []
# localhost:8080/book/id1/ should be [book] [id1]
# localhost:8080/book/id2/ should be [book] [id2]
path = path.strip('/').split('/')
func_name = path[0]
func_args = path[1:]
try:
func = functions[func_name]
except KeyError:
raise NameError
finally:
return func, func_args

def application(environ, start_response):
status = "200 OK"
headers = [('Content-type', 'text/html')]
start_response(status, headers)
return ["<h1>No Progress Yet</h1>".encode('utf8')]

try:
path = environ.get("PATH_INFO", None)
if path is None:
raise NameError
func, args = book_router(path)
body = func(*args)
status = "200 OK"
except NameError:
status = '404 Not Found'
body = '<h1> 404 Not Found </h1>'
except Exception:
status = '500 Internal Server Error'
body = '<h1> Internal Server Error </h1>'
print(traceback.format_exc())
finally:
headers.append(('Content-len', str(len(body))))
start_response(status, headers)
return [body.encode('utf8')]

if __name__ == '__main__':
from wsgiref.simple_server import make_server
Expand Down