look at the handler
in the method test in class case_no_file in the following code snippet. What is this handler and why can the .full _ path attribute be called?
and can the handle_file method be called in class case_existing_file?
full_path,handle_file is defined in class RequestHandler, but how does the code associate handler with RequestHandler?
-sharp-*- coding:utf-8 -*-
import sys, os
from http.server import BaseHTTPRequestHandler,HTTPServer
class ServerException(Exception):
""""""
pass
class case_no_file(object):
""""""
def test(self, handler):
return not os.path.exists(handler.full_path)
def act(self, handler):
raise ServerException(""{0}" not found".format(handler.path))
class case_existing_file(object):
""""""
def test(self, handler):
return os.path.isfile(handler.full_path)
def act(self, handler):
handler.handle_file(handler.full_path)
class case_always_fail(object):
""""""
def test(self, handler):
return True
def act(self, handler):
raise ServerException("Unknown object "{0}"".format(handler.path))
class case_directory_index_file(object):
def index_path(self, handler):
return os.path.join(handler.full_path, "index.html")
-sharp&&index.html
def test(self, handler):
return os.path.isdir(handler.full_path) and \
os.path.isfile(self.index_path(handler))
-sharpindex.html
def act(self, handler):
handler.handle_file(self.index_path(handler))
class RequestHandler(BaseHTTPRequestHandler):
"""
"""
Cases = [case_no_file(),
case_existing_file(),
case_directory_index_file(),
case_always_fail()]
-sharp
Error_Page = """\
<html>
<body>
<h1>Error accessing {path}</h1>
{msg}
</body>
</html>
"""
def do_GET(self):
try:
-sharp
self.full_path = os.getcwd() + self.path
-sharp
for case in self.Cases:
if case.test(self):
case.act(self)
break
-sharp
except Exception as msg:
self.handle_error(msg)
def handle_error(self, msg):
content = self.Error_Page.format(path=self.path, msg=msg)
self.send_content(content.encode("utf-8"), 404)
-sharp
def send_content(self, content, status=200):
self.send_response(status)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(len(content)))
self.end_headers()
self.wfile.write(content)
def handle_file(self, full_path):
try:
with open(full_path, "rb") as reader:
content = reader.read()
self.send_content(content)
except IOError as msg:
msg = ""{0}" cannot be read: {1}".format(self.path, msg)
self.handle_error(msg)
if __name__ == "__main__":
serverAddress = ("", 8080)
server = HTTPServer(serverAddress, RequestHandler)
server.serve_forever()