Skip to content

Commit

Permalink
Updating based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
gvwilson committed May 27, 2014
1 parent f502333 commit 17933a9
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 169 deletions.
14 changes: 7 additions & 7 deletions web-server/01-echo-request-info/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
<html>
<body>
<table>
<tr> <td>Header</td> <td>Value</td> </tr>
<tr> <td>Date and time</td> <td>%(date_time)s</td> </tr>
<tr> <td>Client host</td> <td>%(client_host)s</td> </tr>
<tr> <td>Client port</td> <td>%(client_port)s</td> </tr>
<tr> <td>Command</td> <td>%(command)s</td> </tr>
<tr> <td>Path</td> <td>%(path)s</td> </tr>
<tr> <td>Header</td> <td>Value</td> </tr>
<tr> <td>Date and time</td> <td>{date_time}</td> </tr>
<tr> <td>Client host</td> <td>{client_host}</td> </tr>
<tr> <td>Client port</td> <td>{client_port}s</td> </tr>
<tr> <td>Command</td> <td>{command}</td> </tr>
<tr> <td>Path</td> <td>{path}</td> </tr>
</table>
</body>
</html>
Expand All @@ -36,7 +36,7 @@ def create_page(self):
'command' : self.command,
'path' : self.path
}
page = self.Page % values
page = self.Page.format(**values)
return page

# Send the created page.
Expand Down
21 changes: 10 additions & 11 deletions web-server/02-serve-static/server-status-code.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
Error_Page = """\
<html>
<body>
<h1>Error accessing %(path)s</h1>
<p>%(msg)s</p>
<h1>Error accessing {path}</h1>
<p>{msg}</p>
</body>
</html>
"""
Expand All @@ -33,33 +33,32 @@ def do_GET(self):

# It doesn't exist...
if not os.path.exists(full_path):
raise ServerException("'%s' not found" % self.path)
raise ServerException("'{0}' not found".format(self.path))

# ...it's a file...
elif os.path.isfile(full_path):
self.handle_file(full_path)

# ...it's something we don't handle.
else:
raise ServerException("Unknown object '%s'" % self.path)
raise ServerException("Unknown object '{0}'".format(self.path))

# Handle errors.
except Exception, msg:
except Exception as msg:
self.handle_error(msg)

def handle_file(self, full_path):
try:
with open(full_path, 'r') as input:
content = input.read()
with open(full_path, 'rb') as reader:
content = reader.read()
self.send_content(content)
except IOError, msg:
msg = "'%s' cannot be read: %s" % (self.path, msg)
except IOError as msg:
msg = "'{0}' cannot be read: {1}".format(self.path, msg)
self.handle_error(msg)

# Handle unknown objects.
def handle_error(self, msg):
content = self.Error_Page % {'path' : self.path,
'msg' : msg}
content = self.Error_Page.format(path=self.path, msg=msg)
self.send_content(content, 404)

# Send actual content.
Expand Down
21 changes: 10 additions & 11 deletions web-server/02-serve-static/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
Error_Page = """\
<html>
<body>
<h1>Error accessing %(path)s</h1>
<p>%(msg)s</p>
<h1>Error accessing {path}</h1>
<p>{msg}</p>
</body>
</html>
"""
Expand All @@ -33,33 +33,32 @@ def do_GET(self):

# It doesn't exist...
if not os.path.exists(full_path):
raise ServerException("'%s' not found" % self.path)
raise ServerException("'{0}' not found".format(self.path))

# ...it's a file...
elif os.path.isfile(full_path):
self.handle_file(full_path)

# ...it's something we don't handle.
else:
raise ServerException("Unknown object '%s'" % self.path)
raise ServerException("Unknown object '{0}'".format(self.path))

# Handle errors.
except Exception, msg:
except Exception as msg:
self.handle_error(msg)

def handle_file(self, full_path):
try:
with open(full_path, 'r') as input:
content = input.read()
with open(full_path, 'rb') as reader:
content = reader.read()
self.send_content(content)
except IOError, msg:
msg = "'%s' cannot be read: %s" % (self.path, msg)
except IOError as msg:
msg = "'{0}' cannot be read: {1}".format(self.path, msg)
self.handle_error(msg)

# Handle unknown objects.
def handle_error(self, msg):
content = self.Error_Page % {'path' : self.path,
'msg' : msg}
content = self.Error_Page.format(path=self.path, msg=msg)
self.send_content(content)

# Send actual content.
Expand Down
21 changes: 10 additions & 11 deletions web-server/03-handlers/server-index-page.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test(self, handler):
return not os.path.exists(handler.full_path)

def act(self, handler):
raise ServerException("'%s' not found" % handler.path)
raise ServerException("'{0}' not found".format(handler.path))

#-------------------------------------------------------------------------------

Expand Down Expand Up @@ -52,7 +52,7 @@ def test(self, handler):
return True

def act(self, handler):
raise ServerException("Unknown object '%s'" % handler.path)
raise ServerException("Unknown object '{0}'".format(handler.path))

#-------------------------------------------------------------------------------

Expand All @@ -71,8 +71,8 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
Error_Page = """\
<html>
<body>
<h1>Error accessing %(path)s</h1>
<p>%(msg)s</p>
<h1>Error accessing {path}</h1>
<p>{msg}</p>
</body>
</html>
"""
Expand All @@ -91,22 +91,21 @@ def do_GET(self):
break

# Handle errors.
except Exception, msg:
except Exception as msg:
self.handle_error(msg)

def handle_file(self, full_path):
try:
with open(full_path, 'r') as input:
content = input.read()
with open(full_path, 'rb') as reader:
content = reader.read()
self.send_content(content)
except IOError, msg:
msg = "'%s' cannot be read: %s" % (self.path, msg)
except IOError as msg:
msg = "'{0}' cannot be read: {1}".format(self.path, msg)
self.handle_error(msg)

# Handle unknown objects.
def handle_error(self, msg):
content = self.Error_Page % {'path' : self.path,
'msg' : msg}
content = self.Error_Page.format(path=self.path, msg=msg)
self.send_content(content, 404)

# Send actual content.
Expand Down
31 changes: 15 additions & 16 deletions web-server/03-handlers/server-no-index-page.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test(self, handler):
return not os.path.exists(handler.full_path)

def act(self, handler):
raise ServerException("'%s' not found" % handler.path)
raise ServerException("'{0}' not found".format(handler.path))

#-------------------------------------------------------------------------------

Expand Down Expand Up @@ -67,7 +67,7 @@ def test(self, handler):
return True

def act(self, handler):
raise ServerException("Unknown object '%s'" % handler.path)
raise ServerException("Unknown object '{0}'".format(handler.path))

#-------------------------------------------------------------------------------

Expand All @@ -87,8 +87,8 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
Error_Page = """\
<html>
<body>
<h1>Error accessing %(path)s</h1>
<p>%(msg)s</p>
<h1>Error accessing {path}</h1>
<p>{msg}</p>
</body>
</html>
"""
Expand All @@ -98,7 +98,7 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
<html>
<body>
<ul>
%s
{0}
</ul>
</body>
</html>
Expand All @@ -118,32 +118,31 @@ def do_GET(self):
break

# Handle errors.
except Exception, msg:
except Exception as msg:
self.handle_error(msg)

def handle_file(self, full_path):
try:
with open(full_path, 'r') as input:
content = input.read()
with open(full_path, 'rb') as reader:
content = reader.read()
self.send_content(content)
except IOError, msg:
msg = "'%s' cannot be read: %s" % (self.path, msg)
except IOError as msg:
msg = "'{0}' cannot be read: {1}".format(self.path, msg)
self.handle_error(msg)

def list_dir(self, full_path):
try:
entries = os.listdir(full_path)
bullets = ['<li>%s</li>' % e for e in entries if not e.startswith('.')]
page = self.Listing_Page % '\n'.join(bullets)
bullets = ['<li>{0}</li>'.format(e) for e in entries if not e.startswith('.')]
page = self.Listing_Page.format('\n'.join(bullets))
self.send_content(page)
except OSError, msg:
msg = "'%s' cannot be listed: %s" % (self.path, msg)
except OSError as msg:
msg = "'{0}' cannot be listed: {1}".format(self.path, msg)
self.handle_error(msg)

# Handle unknown objects.
def handle_error(self, msg):
content = self.Error_Page % {'path' : self.path,
'msg' : msg}
content = self.Error_Page.format(path=self.path, msg=msg)
self.send_content(content, 404)

# Send actual content.
Expand Down
21 changes: 10 additions & 11 deletions web-server/03-handlers/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test(self, handler):
return not os.path.exists(handler.full_path)

def act(self, handler):
raise ServerException("'%s' not found" % handler.path)
raise ServerException("'{0}' not found".format(handler.path))

#-------------------------------------------------------------------------------

Expand All @@ -37,7 +37,7 @@ def test(self, handler):
return True

def act(self, handler):
raise ServerException("Unknown object '%s'" % handler.path)
raise ServerException("Unknown object '{0}'".format(handler.path))

#-------------------------------------------------------------------------------

Expand All @@ -55,8 +55,8 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
Error_Page = """\
<html>
<body>
<h1>Error accessing %(path)s</h1>
<p>%(msg)s</p>
<h1>Error accessing {path}</h1>
<p>{msg}</p>
</body>
</html>
"""
Expand All @@ -75,22 +75,21 @@ def do_GET(self):
break

# Handle errors.
except Exception, msg:
except Exception as msg:
self.handle_error(msg)

def handle_file(self, full_path):
try:
with open(full_path, 'r') as input:
content = input.read()
with open(full_path, 'rb') as reader:
content = reader.read()
self.send_content(content)
except IOError, msg:
msg = "'%s' cannot be read: %s" % (self.path, msg)
except IOError as msg:
msg = "'{0}' cannot be read: {1}".format(self.path, msg)
self.handle_error(msg)

# Handle unknown objects.
def handle_error(self, msg):
content = self.Error_Page % {'path' : self.path,
'msg' : msg}
content = self.Error_Page.format(path=self.path, msg=msg)
self.send_content(content, 404)

# Send actual content.
Expand Down
Loading

0 comments on commit 17933a9

Please sign in to comment.