mini_frame框架

import re
import logging
from urllib import parse
from pymysql import *

# URL_FUNC_DICT = {
# "/index.py": index,
# "/center.py": center
# }

URL_FUNC_DICT = dict()


def roo_te(path):
def ret_func(func):
URL_FUNC_DICT[path] = func

def call_func(*args, **kwargs):
return func(*args, **kwargs)

return call_func

return ret_func


@roo_te(r"/index.html")
def index(ret):
"""主页"""
with open("./templates/index.html") as f:
content = f.read()

co = connect(host="localhost",
port=3306,
database="stock_db",
user="root",
password="mysql",
charset="utf8")
coon = co.cursor()

sql = """select * from info;"""
coon.execute(sql)

stock_info = coon.fetchall()
# print(stock_info)
coon.close()
co.close()

html_template = """
<tr>
<td>%d</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>"""
html = ""
for line_info in stock_info:
html += html_template % (line_info[0], line_info[1], line_info[2],
line_info[3], line_info[4], line_info[5],
line_info[6], line_info[7], line_info[1])

content = re.sub(r"{%content%}", html, content)

return content


@roo_te(r"/center.html")
def center(ret):
with open("./templates/center.html") as f:
content = f.read()
# print(1)
co = connect(host="localhost",
port=3306,
database="stock_db",
user="root",
password="mysql",
charset="utf8")

coon = co.cursor()
sql = """select * from v_in_fo;"""
# print(2)
coon.execute(sql)

stock_info = coon.fetchall()
# print(stock_info)
coon.close()
co.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>
"""
html = ""
for line_info in stock_info:
html += tr_template % (line_info[0], line_info[1], line_info[2],
line_info[3], line_info[4], line_info[5],
line_info[7], line_info[0], line_info[0])
content = re.sub(r"{%content%}", html, content)

return content


@roo_te(r"/add/(d+).html")
def add_focus(ret):
# 提取股票代码
stock_code = ret.group(1)
# 链接数据库
co = connect(host="localhost",
port=3306,
database="stock_db",
user="root",
password="mysql",
charset="utf8")

coon = co.cursor()
sql = """select * from v_in_fo where code=%s """
re_t = coon.execute(sql, (stock_code,))
# 根据股票代码判断有没有在个人中心
if re_t:
# 有:放回已经关注过,不要重复关注
coon.close()
co.close()
return "已经关注了,请勿重新关注"
# 没有:则在个人中心insert一条数据

sql = """insert into focus(info_id) select id from info where code=%s;"""
coon.execute(sql, (stock_code,))
co.commit()
# 关闭数据库
coon.close()
co.close()
return "关注成功"


@roo_te(r"/del/(d+).html")
def del_focus(ret):
stock_code = ret.group(1)
# 链接数据库
print(stock_code)
co = connect(host="localhost",
port=3306,
database="stock_db",
user="root",
password="mysql",
charset="utf8")

coon = co.cursor()
sql = """select * from v_in_fo where code=%s; """
re_t = coon.execute(sql, (stock_code,))
print(re_t)
# 根据股票代码判断有没有在个人中心
# print(stock_code)
if re_t:
sql = """delete from focus where info_id=(select id from info where code=%s);"""
coon.execute(sql, (stock_code,))
co.commit()
# 关闭数据库
coon.close()
co.close()
return "删除成功"
# 没有:则在个人中心insert一条数据
else:
coon.close()
co.close()
return "没有关注,请关注"


@roo_te(r"/update/(d+).html")
def show_update_focus(ret):
"""显示修改页面"""
stock_code = ret.group(1)

with open("./templates/update.html") as f:
content = f.read()

co = connect(host="localhost",
port=3306,
database="stock_db",
user="root",
password="mysql",
charset="utf8")

coon = co.cursor()
sql = """select note_info from focus where info_id=(select id from info where code=%s);"""
coon.execute(sql, (stock_code,))
note_info_tuple = coon.fetchone()
coon.close()
co.close()
for note_info in note_info_tuple:
content = re.sub(r"{%note_info%}", note_info, content)
content = re.sub(r"{%code%}", stock_code, content)

return content


@roo_te(r"/update/(d+)/(.*).html")
def update_focus(ret):
"""修改备注消息"""
stock_code = ret.group(1)
comant = ret.group(2)
co_ment = parse.unquote(comant)

co = connect(host="localhost",
port=3306,
database="stock_db",
user="root",
password="mysql",
charset="utf8")

coon = co.cursor()
sql = """update focus set note_info=%s where info_id=(select id from info where code=%s);"""
coon.execute(sql, (co_ment, stock_code))
co.commit()
coon.close()
co.close()
return "修改成功"


def application(environ, start_reponse):
start_reponse("200 OK", [("Content-Type", "text/html:charset=utf-8")])
file_name = environ["PATH_INFO"]
# if file_name == "/index.py":
# return index()
# elif file_name == "/center.py":
# return center()
# else:
# return "Hello World! wo ai ni zhong guo"

logging.basicConfig(level=logging.INFO,
filename="./log.txt",
filemode="a",
format="%(asctime)s-"
"%(filename)s[line:%(lineno)d]-"
"%(levelname)s:%(message)s")

logging.info("访问的是:%s" % file_name)

try:
for url, func in URL_FUNC_DICT.items():
ret = re.match(url, file_name)
if ret:
return func(ret)

else:
logging.warning("没有对应的函数")
return "not found %s" % file_name
except Exception as ret:
return "error:%s" % ret

原文地址:https://www.cnblogs.com/hm-hx-ldy/p/8583056.html