自定义web框架
框架功能
"""
功能视图
用户访问指定url 获取数据库数据,并返回到页面
"""

wsgiref模块
"""
urls.py 路由与视图函数对应关系
views.py 视图函数(后端业务逻辑)
templates文件夹 专门用来存储html文件
"""
服务端
from wsgiref.simple_server import make_server
from urls import urls
from views import *
def run(env, response):
"""
:param env: 请求数据
:param response: 响应数据
:return:
"""
response('200 OK', [])
curr_path = env.get('PATH_INFO')
func = None
for uri in urls:
if curr_path == uri[0]:
print(curr_path,uri)
func = uri[1]
break
if func:
res = func(env)
else:
res = error(env)
return [res.encode('utf-8')]
if __name__ == '__main__':
server = make_server('127.0.0.1', 8080, run)
server.serve_forever()
路由 urls.py
"""
路由于视图关系
"""
from views import *
urls = [
('/index',index),
('/login',login),
('/error',error),
('/xxx',file_tmp),
('/get_user',get_user)
]
视图函数 后端处理views.py
"""
视图函数
"""
def index(env):
return "index hi"
def login(env):
return "login"
def error(env):
return "404 error"
def file_tmp(env):
with open(r'templates/xxx.html', 'r', encoding='utf-8') as f:
return f.read()
from jinja2 import Template
def get_user(env):
user_dic = {'username': 'futongxue', 'age': 18}
with open(r'templates/02-get_user.html', 'r', encoding='utf-8') as f:
data = f.read()
tmp = Template(data)
res = tmp.render(user=user_dic)
return res
import pymysql
def getsql(env):
conn = pymysql.connect(
user='root',
host='192.168.5.9',
password='0x00NF2001',
port=3306,
database='note',
charset='utf8',
autocommit=True
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = 'SELECT * FROM `user`;'
affect_rows = cursor.execute(sql)
data_dic = cursor.fetchall()
with open(r'templates/02-usersql.html', 'r', encoding='utf-8') as f:
data = f.read()
tmp = Template(data)
res = tmp.render(user_list=data_dic)
return res
模版语法之Jinja2模块
pip3 install jinja2
"""模版语法是在后端起作用的"""
{{ user }}
{{ user.get('username')}}
{{ user.age }}
{{ user['hobby'] }}
{% for user_dict in user_list %}
<tr>
<td>{{ user_dict.id}}</td>
<td>{{ user_dict.username}}</td>
<td>{{ user_dict.password}}</td>
<td>{{ user_dict.hobby}}</td>
</tr>
{% endfor%}
templates 页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1 class="text-center">用户数据</h1>
<table class="table table-hover table-striped">
<thead>
<tr>
<th>ID</th>
<th>username</th>
<th>password</th>
<th>hobby</th>
</tr>
</thead>
<tbody>
{% for user_dict in user_list %}
<tr>
<td>{{ user_dict.id}}</td>
<td>{{ user_dict.username}}</td>
<td>{{ user_dict.password}}</td>
<td>{{ user_dict.hobby}}</td>
</tr>
{% endfor%}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~