Skip to content
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

michael_mcdonald submitting lesson4 ex, wscgi #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/wsgi.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added __pycache__/bookdb.cpython-38.pyc
Binary file not shown.
60 changes: 53 additions & 7 deletions bookapp.py
Original file line number Diff line number Diff line change
@@ -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 "<h1>a book with id %s</h1>" % book_id
page = """
<h1>{title}</h1>
<table>
<tr><th>Author</th><td>{author}</td></tr>
<tr><th>Publisher</th><td>{publisher}</td></tr>
<tr><th>ISBN</th><td>{isbn}</td></tr>
</table>
<a href="/">Back to the list</a>
"""
book = DB.title_info(book_id)
if book is None:
raise NameError
return page.format(**book)


def books():
return "<h1>a list of books</h1>"
all_books = DB.titles()
body = ['<h1>My Bookshelf</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 application(environ, start_response):
status = "200 OK"
headers = [('Content-type', 'text/html')]
start_response(status, headers)
return ["<h1>No Progress Yet</h1>".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 = "<h1>Not Found</h1>"
except Exception:
status = "500 Internal Server Error"
body = "<h1>Internal Server Error</h1>"
print(traceback.format_exc())
finally:
headers.append(('Content-length', str(len(body))))
start_response(status, headers)
return [body.encode('utf8')]


if __name__ == '__main__':
Expand Down
2 changes: 2 additions & 0 deletions bookdb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

class BookDB():
"""books db"""

def titles(self):
titles = [
dict(id=id, title=database[id]['title']) for id in database.keys()
Expand Down
14 changes: 5 additions & 9 deletions wsgi_1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
import datetime

default = "No Value Set"

body = """<html>
Expand All @@ -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')]


Expand Down