mini web框架-路由功能(支持正则表达式参数)
dynamic/my_web.py
import pymysql
import time
import os
import re
template_root = "./templates"
g_url_route = dict()
def route (url) :
def set_func (func) :
g_url_route[url]=func
def call_func (file_name) :
return func(file_name)
return call_func
return set_func
@route(r"/index.html")
def index (file_name, url=None) :
"""返回index.html需要的页面内容"""
try :
file_name = file_name.replace(".py" , ".html" )
f = open(template_root + file_name)
except Exception as ret:
return "%s" % ret
else :
content = f.read()
f.close()
db = pymysql.connect(host='localhost' ,port=3306 ,user='root' ,password='mysql' ,database='stock_db' ,charset='utf8' )
cursor = db.cursor()
sql = """select * from info;"""
cursor.execute(sql)
data_from_mysql = cursor.fetchall()
cursor.close()
db.close()
html_template = """
%d
%s
%s
%s
%s
%s
%s
%s
"""
html = ""
for info in data_from_mysql:
html += html_template % (info[0 ], info[1 ], info[2 ], info[3 ], info[4 ], info[5 ], info[6 ], info[7 ], info[1 ])
content = re.sub(r"\{%content%\}" , html, content)
return content
@route(r"/center.html")
def center (file_name, url=None) :
"""返回center.html需要的页面内容"""
try :
file_name = file_name.replace(".py" , ".html" )
f = open(template_root + file_name)
except Exception as ret:
return "%s" % ret
else :
content = f.read()
f.close()
db = pymysql.connect(host='localhost' ,port=3306 ,user='root' ,password='mysql' ,database='stock_db' ,charset='utf8' )
cursor = db.cursor()
sql = """select i.code,i.short,i.chg,i.turnover,i.price,i.highs,j.note_info from info as i inner join focus as j on i.id=j.info_id;"""
cursor.execute(sql)
data_from_mysql = cursor.fetchall()
cursor.close()
db.close()
html_template = """
%s
%s
%s
%s
%s
%s
%s
修改
"""
html = ""
for info in data_from_mysql:
html += html_template % (info[0 ], info[1 ], info[2 ], info[3 ], info[4 ], info[5 ], info[6 ], info[0 ], info[0 ])
content = re.sub(r"\{%content%\}" , html, content)
return content
@route(r"/update/(\d*)\.html")
def update (file_name, url) :
"""显示 更新页面的内容"""
try :
template_file_name = template_root + "/update.html"
f = open(template_file_name)
except Exception as ret:
return "%s,,,没有找到%s" % (ret, template_file_name)
else :
content = f.read()
f.close()
ret = re.match(url, file_name)
if ret:
stock_code = ret.group(1 )
return stock_code
def application (environ, set_response_headers) :
status = '200 OK'
response_headers = [('Content-Type' , 'text/html' )]
set_response_headers(status, response_headers)
file_name = environ['PATH_INFO' ]
try :
for url, func in g_url_route.items():
print(url)
ret = re.match(url, file_name)
if ret:
return func(file_name, url)
break
else :
return "没有访问的页面--->%s" % file_name
except Exception as ret:
return "%s" % ret
else :
return str(environ) + '-----404--->%s\n'
g_url_route = dict()
字典内存储着路由的映射
key:为正则表达式
value:为请求的信息,包含数据和模板文件
# 用来存放url路由映射
# url_route = {
# "r'/index.py'":index',
# "r'/center.py'":center,
# "r'/update/(\d*)\.html':update
# }
def route (url) :
def set_func (func) :
g_url_route[url]=func
def call_func (file_name) :
return func(file_name)
return call_func
return set_func
装饰器支持正则表达式@route('正则表达式')
......
@route(r"/update/(\d*)\.html")
def update (file_name, url) :
......
application()
函数需要使用正则表达式分析服务器传递过来的请求,并调用相应的函数传递参数和返回值
......
for url, func in g_url_route.items():
ret = re.match(url, file_name)
if ret:
return func(file_name, url)
......
使用正则表达式匹配出的请求信息在数据库中查询动态数据,与模板文件拼接成响应体返回
......
"""
SQL操作
"""
for info in data_from_mysql:
html += html_template
content = re.sub(r"\{%content%\}" , html, content)
......
mini-web框架-url编码
import urllib.parse
print (urllib.parse.quote("天安门" ))
print (urllib.parse.unquote("%E5 %A4 %A9 %E5 %AE %89 %E9 %97 %A8 " ))
浏览器url传值默认执行编码操作,所以web框架匹配出的数据需要解码才能使用
......
ret = re.match(url, file_name)
if ret:
stock_code = ret.group(1)
stock_note_info = ret.group(2)
stock_note_info = unquote(stock_note_info)
......