wsgire模块的用法:升级版的django推导2

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

转载于:https://www.cnblogs.com/1832921tongjieducn/p/11390990.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值