完整代码
DjangoWeb.py代码
# coding:utf-8
import time
#from MyWebServer import HttpServer
import re
#常量名字要大写
HTML_ROOT_DIR = "./html"
#得到当前系统时间
def get_time(env,start_response):
#调用传入进来的start_response函数,
#并且调用的时候把当运行状态的相关状态给服务器
status = "200 OK"
headers = [
("Content-Type","Text/plain")
]
start_response(status,headers)
#返回前系统时间
return "Django_"+time.ctime()
#得到字符串hello atguigu
def get_hello(env,start_response):
#调用传入进来的start_response函数,
#并且调用的时候把当运行状态的相关状态给服务器
status = "200 OK"
headers = [
("Content-Type","Text/plain")
]
start_response(status,headers)
#返回Django_hello atguigu
return "Django_hello atguigu"
#得到字符串I love you!
def say_love(env,start_response):
#调用传入进来的start_response函数,
#并且调用的时候把当运行状态的相关状态给服务器
status = "200 OK"
headers = [
("Content-Type","Text/plain")
]
start_response(status,headers)
#返回hello atguigu
return "Django_I love you!"
class Application(object):
"""框架的核心部分,也是框架的主体程序,框架是通用的"""
def __init__(self,urls):
#接收路由信息
self.urls = urls
def __call__(self, env, start_response):
#得到要执行的函数(路径)
#可能得到/ctime或者/hello
path = env.get("PATH_INFO","/")
#/static/index.html
if path.startswith("/static"):
# /static/index.html-->/index.html
file_name = path[7:]
try:
# 打开文件读取/html/index.html文件内容,以二进制方式
f = open(HTML_ROOT_DIR + file_name, "rb")
except: # 文件没有找到返回404
# 1.响应状态
statu = "HTTP/1.1 404 Not Found"
# 2.响应头信息
headers = [
("Content-Type", "Text/plain")
]
# 生成响应头
start_response(statu, headers)
# 返回响应体内容
return "Not Found,Sorry!"
else:
# 文件存在,正常处理
file_data = f.read()