mini_frame框架路由支持正则
修改mini_frame.py代码
# coding=UTF-8
import re
from pymysql import connect
URL_FUN_DIC = dict()
def route(url):
def set_func(func):
# 在执行装饰的过程中将参数添加到字典中
URL_FUN_DIC[url] = func
""" 使用正则表达式当作装饰器的参数之后,字典中的数据如下:
{
"/index.html":index,
"/center.html"center,
r"/add/\d+\.html":add_func
}
"""
def call_func(*args, **kwargs):
return func(*args, **kwargs)
return call_func
return set_func
@route("/index.html")
def index(ret):
with open("./templates/index.html") as f:
content = f.read()
# 从数据库中将数据查询出来
conn = connect(host='localhost', port=3306, user='root', password='mysql', database='stock_db',
charset='utf8')
# 获得cursor对象
cs = conn.cursor()
cs.execute("select * from info;")
stock_info_list = cs.fetchall()
cs.close()
conn.close()
# 按照页面要求的格式组装数据
tr_template = """<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>
<input type="button" value="添加" id="toAdd" name="toAdd" systemidvaule="%s">
</td>
</tr>
"""
# 使用正则表达式替换源模版中的数据
from_mysql_data = ""
for data in stock_info_list:
from_mysql_data += tr_template % (data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[1])
# 使用正则表达式将html中的{%content%}替换成模版中的数据
# from_mysql_data = "这里以后放的是从数据库中查询出来的数据"
content = re.sub(r"\{%content%\}", from_mysql_data, content)
return content
@route("/center.html")
def center(ret):
with open("./templates/center.html") as f:
content = f.read()
# 从数据库中将数据查询出来
conn = connect(host='localhost', port=3306, user='root', password='mysql', database='stock_db',charset='utf8')
# 获得cursor对象
cs = conn.cursor()
cs.execute(
"select i.code,i.short,i.chg,i.turnover,i.price,i.highs,f.note_info from info as i inner join focus as f on i.id=f.info_id;")
stock_info_list = cs.fetchall()
cs.close()
conn.close()
# 按照页面要求的格式组装数据
tr_template = """<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>
<a type="button" class="btn btn-default btn-xs" href="/update/%s.html">
<span class="glyphicon glyphicon-star" aria-hidden="true"></span> 修改 </a>
</td>
<td>
<input type="button" value="删除" id="toDel" name="toDel" systemidvaule="%s">
</td>
</tr>
"""
# 使用正则表达式替换源模版中的数据
from_mysql_data = ""
for data in stock_info_list:
from_mysql_data += tr_template % (data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[0], data[0])
# 使用正则表达式将html中的{%content%}替换成模版中的数据
# from_mysql_data = "这里以后放的是从数据库中查询出来的数据"
content = re.sub(r"\{%content%\}", from_mysql_data, content)
return content
@route(r"/add/(\d+)\.html")
def add_func(ret):
# 获取第一个分组(股票的code)
stock_code = ret.group(1)
# 判断当前股票是否存在
conn = connect(host='localhost', port=3306, user='root', password='mysql', database='stock_db', charset='utf8')
# 获取游标
cs = conn.cursor()
sql = """select * from info where code = %s"""
cs.execute(sql, (stock_code,))
# 如果没有当前代码,提示并return
if not cs.fetchone():
cs.close()
conn.close()
return "没有这只股票,请重试"
# 判断当前选中的股票是否已经关注过
sql = """select * from info as i inner join focus as f on i.id=f.info_id where i.code = %s"""
cs.execute(sql, stock_code)
# 如果已经关注了当前代码,提示并return
if cs.fetchone():
cs.close()
conn.close()
return "您已经关注过这只股票,请勿重复关注"
# 可以开始关注操作,添加关注
sql = """insert into focus(info_id) select id from info where code=%s"""
cs.execute(sql, stock_code)
conn.commit()
cs.close()
conn.close()
return "关注 成功 .... "
@route(r"/del/(\d+)\.html")
def del_func(ret):
# 获取第一个分组(股票的code)
stock_code = ret.group(1)
# 判断当前股票是否存在
conn = connect(host='localhost', port=3306, user='root', password='mysql', database='stock_db', charset='utf8')
# 获取游标
cs = conn.cursor()
sql = """select * from info where code = %s"""
cs.execute(sql, (stock_code,))
# 如果没有当前代码,提示并return
if not cs.fetchone():
cs.close()
conn.close()
return "没有这只股票,请重试"
# 判断当前选中的股票是否已经关注过
sql = """select * from info as i inner join focus as f on i.id=f.info_id where i.code = %s"""
cs.execute(sql, stock_code)
# 如果没有关注当前代码,提示并return
if not cs.fetchone():
cs.close()
conn.close()
return "您没有关注过这只股票,取消失败"
# 可以开始取消关注操作,删除关注
sql = """delete from focus where info_id = (select id from info where code=%s)"""
cs.execute(sql, stock_code)
conn.commit()
cs.close()
conn.close()
return "取消关注 成功 .... "
@route(r"/update/(\d+)\.html")
def to_update_view(ret):
"""跳转到修改数据的页面"""
# 获取股票code
stock_code = ret.group(1)
# 打开模版页面
with open("./templates/update.html") as f:
content = f.read()
# 根据股票代码查询相关数据
conn = connect(host='localhost', port=3306, user='root', password='mysql', database='stock_db', charset='utf8')
cs = conn.cursor()
sql = """select f.note_info from focus as f inner join info as i on i.id=f.info_id where i.code=%s;"""
cs.execute(sql, stock_code)
stock_infos = cs.fetchone()
note_info = stock_infos[0] # 获取当前股票的备注信息,用于页面回显
cs.close()
conn.close()
# 替换html页面中的数据
content = re.sub(r"\{%note_info%\}", note_info, content)
content = re.sub(r"\{%code%\}", stock_code, content)
return content
@route(r"/update/(\d+)/(.*)\.html")
def update_stock(ret):
"""修改股票信息"""
stock_code = ret.group(1)
comment = ret.group(2)
conn = connect(host='localhost', port=3306, user='root', password='mysql', database='stock_db', charset='utf8')
cs = conn.cursor()
sql = """update focus set note_info=%s where info_id = (select id from info where code = %s)"""
cs.execute(sql, (comment, stock_code))
conn.commit()
cs.close()
conn.close()
return "修改成功..."
def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')])
#获取的url中输入的路径,file_name = /add/10007.html
file_name = env['PATH_INFO']
try:
# return URL_FUN_DIC[file_name]()
"""
使用正则表达式当作装饰器的参数之后,字典中的数据如下:
{
"/index.html":index,
"/center.html"center,
r"/add/\d+\.html":add_func
}
使用item()方法进行遍历,取出每一个url和func
"""
for url, func in URL_FUN_DIC.items():
ret = re.match(url, file_name)
#用正则对输入url的格式进行匹配
if ret:
return func(ret) #调用上面的函数
else:
return "请求的页面 %s 不存在" % file_name
except Exception as e:
return "您访问的页面 %s 不存在" % str(e)
编写不易,未有VIP但想白嫖文章的朋友可以关注我的个人公众号“不秃头的码农”直接查看文章,后台回复java资料、单片机、安卓、简历可免费领取资源。你的支持是我最大的动力!后期将在公众号发布一些大厂面试题的文章解读!