💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
#### 框架的代码 ~~~ #! /usr/bin/env python # -*- coding:utf-8 -*- ''' @Time: 5/2/183:44 AM @Author:haibo @File: ctime.py ''' import time # from MyWebServer import HTTPServer HTML_ROOT_DIR = "./wwwroot" class Application(object): """Web框架的核心部分,也就是框架的主体程序,框架是通用的""" def __init__(self, urls): # 设置路由信息 self.urls = urls def __call__(self, env, start_response): path = env.get("PATH_INFO", "/") # 判断路径 # /static/index.html if path.startswith("/static"): # 要访问静态文件 file_name = path[7:] # 打开文件,读取内容 try: file = open(HTML_ROOT_DIR + file_name, "rb") except IOError: status = "404 Not Found" headers = [] start_response(status, headers) return "not found" else: file_data = file.read() file.close() status = "200 OK" headers = [] start_response(status, headers) return file_data.decode("utf-8") for url, handler in self.urls: if path == url: return handler(env, start_response) # 代表未找到路由信息,404错误 status = "404 Not Found" headers = [] start_response(status, headers) return "not found" def show_time(env, start_response): status = "200 OK" headers = [ ("Content-Type", "text/plain") ] start_response(status, headers) return time.ctime() def say_hello(env, start_response): status = "200 OK" headers = [ ("Content-Type", "text/plain") ] start_response(status, headers) return "hello haibo" url = [ ("/", show_time), ("/ctime", show_time), ("/sayhello", say_hello), ] # 把自身当做对象给APP app = Application(url) # app = Application(url) # http_server = HTTPServer(app) # http_server.bind(8002) # http_server.start() ~~~