Web服务器--支持多个遵循WSGI的web框架

完整代码

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()

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值