python web框架

impport re
import urllib.parse
import logging
from pymysql import connect

URL_FUNC_DICT = dict()

def route(url):
def set_func(func):
URL_FUNC_DICT[url] = fnc
def call_func(*args, **kwargs):
return (*args, **kwargs)
return call_func
return set_func

@route(r"/index.html")
def indext(ret):
with open("./templates/index,html") as f:
content = f.read()

# 创建connection链接
conn = connect(host='localhost',port=3306,user='root',password='xxxxxx',database='stock_db',charset='utf8')
# 获得cursor对象
cs = conn.cusor()
cs.execute("select * from info;")
stock_infos = 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>
"""
html = ""
for line_info in stock_infos:
	html += tr_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

@ route(r"/center.html")
def center(ret):
with open("./templates/center.html") as f:
content = f.read()
conn = connect(‘localhost’,port=3306,user=‘root’,password=‘xxxxxx’,database=‘stock_db’,charset=‘utf8’)
cs = conn.cursor()
cs.execute(“select i.code,i.short,i.chg,i.trnover,i.price,i.highs,f.note_info from info as i inner join focus as f on i.id=f.info_id;”)
stock_infos = cs,fetchall()
cs.close()
conn.close()
tr_template"""

%s
%s
%s
%s
%s
%s
%s

修改


“”"
html = “”
for line_info in stock_infos:
html += tr_template % (line_info[0],line_info[1],line_info[2],line_info[3],line_info[4],line_info[5],line_info[6],line_info[0],line_info[0])
content = re.sub(r"{%content%}",html,content)
return content

@rout(r"/add/(\d+).html")
def add_focus(ret):
stock_code =ret.group(1)
onn = connect(host=‘localhost’,port=3306,user=‘root’,password=‘xxxxxx’,database=‘stock_ db’,charset=‘utf8’)
cs = conn.cursor()
sql = “”“select * from info where code=%s;”""
cs.execute(sql, (stock_code,))
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,))
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_focus(ret):
stock_code = ret.group(1)
conn = connect(host=‘localhost’,port=3306,user=‘root’,password=‘xxxxxx’,database=‘stock_ db’,charset=‘utf8’)
cs = conn.cursor()
sql = “”“select * from info where code=%s;”""
cs.execute(sql,(stock_code,))
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,))
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 show_update_page(ret):
stock_code = ret.group(1)
with open(".templates/update.thml") as f:
content = f.read()

conn = connect(host='localhost',port=3306,user='root',password='xxxxxx',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 wher    e i.code=%s;"""
cs.execute(sql, (stock_code,))
stock_infos = cs.fetchone()
note_info = stock_infos[0]
cs.close()
conn.close()
content =  re.sub(e"\{%mote_info%\}",note_info,content)
content = re.sub(r"\{%%code\}",stock_code,content)
return content

@route(r"/update/(\d+)/(。*)、html")
def save_page(ret):
stock_code = ret.group(1)
comment = ret.group(2)
comment = urllib.parse.unquote(comment)
conn = connect(host=‘localhost’,port=3306,user=‘root’,password=‘xxxxxx’,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=utf8’)])
file_name = env[‘PATH_INFO’]
llogging.bassicConfig(leve=logging.INFO,
filename=’./log.txt’,
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(rul, file_name)
if ret:
return func(ret)
ele:
logging.warning(“没有对应的函数…”)
return “请求的rul(%s)没有对应的函数…” % file_name
except Exception as ret:
return “产生了异常” % str(ret)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值