from wsgiref.simple_server import make_server
# from urls import url
# from views import *
from jinja2 import Template
import pymysql
import time
def index(env):
return 'index'
def reg(env):
return 'reg'
def login(env):
return 'login'
def error(env):
return '404'
def xxx(env):
return 'xxx'
def get_time(env):
with open('templates/show_time.html','r',encoding='utf-8') as f:
data = f.read()
current_time = time.strftime('%Y-%m-%d %X')
res = data.replace('sadklsajdkljsakld',current_time)
return res
def get_user(env):
user_dict = {'username':'jason','password':'123','hobby':[1,2]}
with open('templates/get_user.html','r',encoding='utf-8') as f:
data = f.read()
tmp = Template(data) # 实例化产生对象
res = tmp.render(data = user_dict) # 将user_dict传递给前端页面 前端页面用过变量名data就能够拿到user_dict字典
return res
def get_data(env):
conn = pymysql.connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
password = '123',
database = 'day19',
charset = 'utf8',
autocommit = True
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
cursor.execute('select * from userinfo')
res = cursor.fetchall()
with open('templates/get_data.html','r',encoding='utf8') as f:
data = f.read()
tmp = Template(data)
res1 = tmp.render(user_list = res) # [{},{},{}]
return res1
url = [
('/index',index),
('/reg',reg),
('/login',login),
('/xxx',xxx),
('/get_time',get_time),
('/get_user',get_user),
('/get_data',get_data)
]
def run(env,response):
response('200 OK', []) # 固定格式 不需掌握
print(env) # 将http格式的数据处理完毕 形成一个字典给你调用
current_path = env.get('PATH_INFO')
func = None
for url_tuple in url:
if current_path == url_tuple[0]:
func = url_tuple[1] # 如果路由匹配上了 就回去对应的函数
break
if func:
res