mini-web框架 添加log日志
阅读目录:
一.mini-web框架-路由支持正则
import pymysql
import time
import os
import re template_root = "./templates" # 用来存放url路由映射
# url_route = {
# "/index.py":index_func,
# "/center.py":center_func
# }
g_url_route = dict() def route(url):
def func1(func):
# 添加键值对,key是需要访问的url,value是当这个url需要访问的时候,需要调用的函数引用
g_url_route[url]=func
def func2(file_name):
return func(file_name)
return func2
return func1 @route(r"/index.html")
def index(file_name, url=None):
"""返回index.html需要的页面内容"""
# return "hahha" + os.getcwd() # for test 路径问题
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() # data_from_mysql = "暂时没有数据,请等待学习mysql吧,学习完mysql之后,这里就可以放入mysql查询到的数据了"
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 = """
<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 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需要的页面内容"""
# return "hahha" + os.getcwd() # for test 路径问题
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() # data_from_mysql = "暂时没有数据,,,,~~~~(>_<)~~~~ "
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 = """
<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 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) # 将提取到的股票编码返回到浏览器中,以检查是否能够正确的提取url的数据
return stock_code def app(environ, start_response):
status = '200 OK'
response_headers = [('Content-Type', 'text/html')]
start_response(status, response_headers) file_name = environ['PATH_INFO']
try:
for url, call_func in g_url_route.items():
print(url)
ret = re.match(url, file_name)
if ret:
return call_func(file_name, url)
break
else:
return "没有访问的页面--->%s" % file_name except Exception as ret:
return "%s" % ret else:
return str(environ) + '-----404--->%s\n'
二.mini-web框架-mysql-增
my_web.py(修改)
import pymysql
import time
import os
import re
import sys
from urllib.parse import unquote template_root = "./templates" # 用来存放url路由映射
# url_route = {
# "/index.py":index_func,
# "/center.py":center_func
# }
g_url_route = dict() def route(url):
def func1(func):
# 添加键值对,key是需要访问的url,value是当这个url需要访问的时候,需要调用的函数引用
g_url_route[url]=func
def func2(file_name):
return func(file_name)
return func2
return func1 @route(r"/index.html")
def index(file_name, url=None):
"""返回index.py需要的页面内容"""
# return "hahha" + os.getcwd() # for test 路径问题
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() # data_from_mysql = "暂时没有数据,请等待学习mysql吧,学习完mysql之后,这里就可以放入mysql查询到的数据了"
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 = """
<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 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.py需要的页面内容"""
# return "hahha" + os.getcwd() # for test 路径问题
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() # data_from_mysql = "暂时没有数据,,,,~~~~(>_<)~~~~ "
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 = """
<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 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)
else:
stock_code = 0 db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor()
# 会出现sql注入,怎样修改呢? 参数化
sql = """select focus.note_info from focus inner join info on focus.info_id=info.id where info.code=%s;""" % stock_code
cursor.execute(sql)
stock_note_info = cursor.fetchone()
cursor.close()
db.close() content = re.sub(r"\{%code%\}", stock_code, content)
content = re.sub(r"\{%note_info%\}", str(stock_note_info[0]), content) return content @route(r"/update/(\d*)/(.*)\.html")
def update_note_info(file_name, url):
"""进行数据的真正更新"""
stock_code = 0
stock_note_info = "" 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) db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor()
# 会出现sql注入,怎样修改呢? 参数化
sql = """update focus inner join info on focus.info_id=info.id set focus.note_info="%s" where info.code=%s;""" % (stock_note_info, stock_code)
cursor.execute(sql)
db.commit()
cursor.close()
db.close() return "修改成功" # ------ 添加 -------
@route(r"/add/(\d*)\.html")
def add(file_name, url):
"""添加关注""" stock_code = 0 ret = re.match(url, file_name)
if ret:
stock_code = ret.group(1) db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor() # 判断是否已经关注
sql = """select * from focus inner join info on focus.info_id=info.id where info.code=%s;""" % (stock_code)
cursor.execute(sql)
if cursor.fetchone():
cursor.close()
db.close()
return "已经关注过了,请不要重复关注" # 如果没有关注,那么就进行关注
sql = """insert into focus (info_id) select id from info where code="%s";""" % (stock_code)
cursor.execute(sql)
db.commit()
cursor.close()
db.close() return "关注成功" def app(environ, start_response):
status = '200 OK'
response_headers = [('Content-Type', 'text/html')]
start_response(status, response_headers) file_name = environ['PATH_INFO']
try:
for url, call_func in g_url_route.items():
print(url)
ret = re.match(url, file_name)
if ret:
return call_func(file_name, url)
break else:
return "没有访问的页面--->%s" % file_name except Exception as ret:
return "%s" % ret else:
return str(environ) + '-----404--->%s\n'
三.mini-web框架-mysql-删
my_web.py(修改)
import pymysql
import time
import os
import re
import sys
from urllib.parse import unquote template_root = "./templates" # 用来存放url路由映射
# url_route = {
# "/index.py":index_func,
# "/center.py":center_func
# }
g_url_route = dict() def route(url):
def func1(func):
# 添加键值对,key是需要访问的url,value是当这个url需要访问的时候,需要调用的函数引用
g_url_route[url]=func
def func2(file_name):
return func(file_name)
return func2
return func1 @route(r"/index.html")
def index(file_name, url=None):
"""返回index.py需要的页面内容"""
# return "hahha" + os.getcwd() # for test 路径问题
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() # data_from_mysql = "暂时没有数据,请等待学习mysql吧,学习完mysql之后,这里就可以放入mysql查询到的数据了"
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 = """
<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 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.py需要的页面内容"""
# return "hahha" + os.getcwd() # for test 路径问题
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() # data_from_mysql = "暂时没有数据,,,,~~~~(>_<)~~~~ "
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 = """
<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 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)
else:
stock_code = 0 db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor()
# 会出现sql注入,怎样修改呢? 参数化
sql = """select focus.note_info from focus inner join info on focus.info_id=info.id where info.code=%s;""" % stock_code
cursor.execute(sql)
stock_note_info = cursor.fetchone()
cursor.close()
db.close() content = re.sub(r"\{%code%\}", stock_code, content)
content = re.sub(r"\{%note_info%\}", str(stock_note_info[0]), content) return content @route(r"/update/(\d*)/(.*)\.html")
def update_note_info(file_name, url):
"""进行数据的真正更新"""
stock_code = 0
stock_note_info = "" 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) db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor()
# 会出现sql注入,怎样修改呢? 参数化
sql = """update focus inner join info on focus.info_id=info.id set focus.note_info="%s" where info.code=%s;""" % (stock_note_info, stock_code)
cursor.execute(sql)
db.commit()
cursor.close()
db.close() return "修改成功" @route(r"/add/(\d*)\.html")
def add(file_name, url):
"""添加关注""" stock_code = 0 ret = re.match(url, file_name)
if ret:
stock_code = ret.group(1) db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor() # 判断是否已经关注
sql = """select * from focus inner join info on focus.info_id=info.id where info.code=%s;""" % (stock_code)
cursor.execute(sql)
if cursor.fetchone():
cursor.close()
db.close()
return "已经关注过了,请不要重复关注" # 如果没有关注,那么就进行关注
sql = """insert into focus (info_id) select id from info where code="%s";""" % (stock_code)
cursor.execute(sql)
db.commit()
cursor.close()
db.close() return "关注成功" # ------ 添加 -------
@route(r"/del/(\d*)\.html")
def delete(file_name, url):
"""取消关注""" stock_code = 0 ret = re.match(url, file_name)
if ret:
stock_code = ret.group(1) db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor() # 判断是否已经关注
sql = """select * from focus inner join info on focus.info_id=info.id where info.code=%s;""" % (stock_code)
cursor.execute(sql)
if not cursor.fetchone():
cursor.close()
db.close()
return "并没有关注,为什么要取消关注呢?不理解,啦啦啦。。。" # 如果有关注,那么就进行取消关注
sql = """delete from focus where info_id = (select id from info where code="%s");""" % (stock_code)
cursor.execute(sql)
db.commit()
cursor.close()
db.close() return "取消关注成功" def app(environ, start_response):
status = '200 OK'
response_headers = [('Content-Type', 'text/html')]
start_response(status, response_headers) file_name = environ['PATH_INFO']
try:
for url, call_func in g_url_route.items():
print(url)
ret = re.match(url, file_name)
if ret:
return call_func(file_name, url)
break else:
return "没有访问的页面--->%s" % file_name except Exception as ret:
return "%s" % ret else:
return str(environ) + '-----404--->%s\n'
四.mini-web框架-mysql-改
my_web.py(修改)
import pymysql
import time
import os
import re
import sys template_root = "./templates" # 用来存放url路由映射
# url_route = {
# "/index.py":index_func,
# "/center.py":center_func
# }
g_url_route = dict() def route(url):
def func1(func):
# 添加键值对,key是需要访问的url,value是当这个url需要访问的时候,需要调用的函数引用
g_url_route[url]=func
def func2(file_name):
return func(file_name)
return func2
return func1 @route(r"/index.html")
def index(file_name, url=None):
"""返回index.py需要的页面内容"""
# return "hahha" + os.getcwd() # for test 路径问题
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() # data_from_mysql = "暂时没有数据,请等待学习mysql吧,学习完mysql之后,这里就可以放入mysql查询到的数据了"
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 = """
<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 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.py需要的页面内容"""
# return "hahha" + os.getcwd() # for test 路径问题
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() # data_from_mysql = "暂时没有数据,,,,~~~~(>_<)~~~~ "
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 = """
<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 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)
else:
stock_code = 0 # ------ 添加 -------
db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor()
# 会出现sql注入,怎样修改呢? 参数化
sql = """select focus.note_info from focus inner join info on focus.info_id=info.id where info.code=%s;""" % stock_code
cursor.execute(sql)
stock_note_info = cursor.fetchone()
cursor.close()
db.close() # ------ 修改 -------
content = re.sub(r"\{%code%\}", stock_code, content)
content = re.sub(r"\{%note_info%\}", str(stock_note_info[0]), content) return content # ------ 添加 -------
@route(r"/update/(\d*)/(.*)\.html")
def update_note_info(file_name, url):
"""进行数据的真正更新"""
stock_code = 0
stock_note_info = "" ret = re.match(url, file_name)
if ret:
stock_code = ret.group(1)
stock_note_info = ret.group(2) db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor()
# 会出现sql注入,怎样修改呢? 参数化
sql = """update focus inner join info on focus.info_id=info.id set focus.note_info="%s" where info.code=%s;""" % (stock_note_info, stock_code)
cursor.execute(sql)
db.commit()
cursor.close()
db.close() return "修改成功" def app(environ, start_response):
status = '200 OK'
response_headers = [('Content-Type', 'text/html')]
start_response(status, response_headers) file_name = environ['PATH_INFO']
try:
for url, call_func in g_url_route.items():
print(url)
ret = re.match(url, file_name)
if ret:
return call_func(file_name, url)
break else:
return "没有访问的页面--->%s" % file_name except Exception as ret:
return "%s" % ret else:
return str(environ) + '-----404--->%s\n'
五.mini-web框架-url编码
python3对url编解码
import urllib.parse
# Python3 url编码
print(urllib.parse.quote("天安门"))
# Python3 url解码
print(urllib.parse.unquote("%E5%A4%A9%E5%AE%89%E9%97%A8"))
my_web.py(修改)
import pymysql
import time
import os
import re
import sys
# ------- 添加 --------
from urllib.parse import unquote template_root = "./templates" # 用来存放url路由映射
# url_route = {
# "/index.py":index_func,
# "/center.py":center_func
# }
g_url_route = dict() def route(url):
def func1(func):
# 添加键值对,key是需要访问的url,value是当这个url需要访问的时候,需要调用的函数引用
g_url_route[url]=func
def func2(file_name):
return func(file_name)
return func2
return func1 @route(r"/index.html")
def index(file_name, url=None):
"""返回index.py需要的页面内容"""
# return "hahha" + os.getcwd() # for test 路径问题
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() # data_from_mysql = "暂时没有数据,请等待学习mysql吧,学习完mysql之后,这里就可以放入mysql查询到的数据了"
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 = """
<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 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.py需要的页面内容"""
# return "hahha" + os.getcwd() # for test 路径问题
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() # data_from_mysql = "暂时没有数据,,,,~~~~(>_<)~~~~ "
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 = """
<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 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)
else:
stock_code = 0 db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor()
# 会出现sql注入,怎样修改呢? 参数化
sql = """select focus.note_info from focus inner join info on focus.info_id=info.id where info.code=%s;""" % stock_code
cursor.execute(sql)
stock_note_info = cursor.fetchone()
cursor.close()
db.close() content = re.sub(r"\{%code%\}", stock_code, content)
content = re.sub(r"\{%note_info%\}", str(stock_note_info[0]), content) return content @route(r"/update/(\d*)/(.*)\.html")
def update_note_info(file_name, url):
"""进行数据的真正更新"""
stock_code = 0
stock_note_info = "" 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) # ------ 添加 ------- db = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor()
# 会出现sql注入,怎样修改呢? 参数化
sql = """update focus inner join info on focus.info_id=info.id set focus.note_info="%s" where info.code=%s;""" % (stock_note_info, stock_code)
cursor.execute(sql)
db.commit()
cursor.close()
db.close() return "修改成功" def app(environ, start_response):
status = '200 OK'
response_headers = [('Content-Type', 'text/html')]
start_response(status, response_headers) file_name = environ['PATH_INFO']
try:
for url, call_func in g_url_route.items():
print(url)
ret = re.match(url, file_name)
if ret:
return call_func(file_name, url)
break else:
return "没有访问的页面--->%s" % file_name except Exception as ret:
return "%s" % ret else:
return str(environ) + '-----404--->%s\n'
六.logging日志模块


开发过程中出现bug是必不可免的,你会怎样debug?从第1行代码开始看么?还是有个文件里面记录着哪里错了更方便呢!!!log日志
Python中有个logging模块可以完成相关信息的记录,在debug时用它往往事半功倍
1. 日志级别
日志一共分成5个等级,从低到高分别是:
- DEBUG
- INFO
- WARNING
- ERROR
- CRITICAL
说明:
- DEBUG:详细的信息,通常只出现在诊断问题上
- INFO:确认一切按预期运行
- WARNING:一个迹象表明,一些意想不到的事情发生了,或表明一些问题在不久的将来(例如。磁盘空间低”)。这个软件还能按预期工作。
- ERROR:更严重的问题,软件没能执行一些功能
- CRITICAL:一个严重的错误,这表明程序本身可能无法继续运行
这5个等级,也分别对应5种打日志的方法: debug 、info 、warning 、error 、critical。默认的是WARNING,当在WARNING或之上时才被跟踪。
2. 日志输出
有两种方式记录跟踪,一种输出控制台,另一种是记录到文件中,如日志文件。
2.1、将日志输出到控制台
比如,log1.py 如下:
import logging logging.basicConfig(level=logging.WARNING,
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') # 开始使用log功能
logging.info('这是 loggging info message')
logging.debug('这是 loggging debug message')
logging.warning('这是 loggging a warning message')
logging.error('这是 an loggging error message')
logging.critical('这是 loggging critical message')
运行结果
2017-11-06 23:07:35,725 - log1.py[line:9] - WARNING: 这是 loggging a warning message
2017-11-06 23:07:35,725 - log1.py[line:10] - ERROR: 这是 an loggging error message
2017-11-06 23:07:35,725 - log1.py[line:11] - CRITICAL: 这是 loggging critical message
说明
通过logging.basicConfig函数对日志的输出格式及方式做相关配置,上面代码设置日志的输出等级是WARNING级别,意思是WARNING级别以上的日志才会输出。另外还制定了日志输出的格式。
注意,只要用过一次log功能再次设置格式时将失效,实际开发中格式肯定不会经常变化,所以刚开始时需要设定好格式
2.2、将日志输出到文件
我们还可以将日志输出到文件,只需要在logging.basicConfig函数中设置好输出文件的文件名和写文件的模式。
log2.py 如下:
import logging logging.basicConfig(level=logging.WARNING,
filename='./log.txt',
filemode='w',
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
# use logging
logging.info('这是 loggging info message')
logging.debug('这是 loggging debug message')
logging.warning('这是 loggging a warning message')
logging.error('这是 an loggging error message')
logging.critical('这是 loggging critical message')
运行效果
python@ubuntu: cat log.txt
2017-11-06 23:10:44,549 - log2.py[line:10] - WARNING: 这是 loggging a warning message
2017-11-06 23:10:44,549 - log2.py[line:11] - ERROR: 这是 an loggging error message
2017-11-06 23:10:44,549 - log2.py[line:12] - CRITICAL: 这是 loggging critical message
2.3、既要把日志输出到控制台, 还要写入日志文件
这就需要一个叫作Logger 的对象来帮忙,下面将对他进行详细介绍,现在这里先学习怎么实现把日志既要输出到控制台又要输出到文件的功能。
import logging # 第一步,创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.INFO) # Log等级总开关 # 第二步,创建一个handler,用于写入日志文件
logfile = './log.txt'
fh = logging.FileHandler(logfile, mode='a') # open的打开模式这里可以进行参考
fh.setLevel(logging.DEBUG) # 输出到file的log等级的开关 # 第三步,再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING) # 输出到console的log等级的开关 # 第四步,定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
ch.setFormatter(formatter) # 第五步,将logger添加到handler里面
logger.addHandler(fh)
logger.addHandler(ch) # 日志
logger.debug('这是 logger debug message')
logger.info('这是 logger info message')
logger.warning('这是 logger warning message')
logger.error('这是 logger error message')
logger.critical('这是 logger critical message')
运行时终端的输出结果:
2017-11-06 23:14:04,731 - log3.py[line:28] - WARNING: 这是 logger warning message
2017-11-06 23:14:04,731 - log3.py[line:29] - ERROR: 这是 logger error message
2017-11-06 23:14:04,731 - log3.py[line:30] - CRITICAL: 这是 logger critical message
在log.txt中,有如下数据:
2017-11-06 23:14:04,731 - log3.py[line:27] - INFO: 这是 logger info message
2017-11-06 23:14:04,731 - log3.py[line:28] - WARNING: 这是 logger warning message
2017-11-06 23:14:04,731 - log3.py[line:29] - ERROR: 这是 logger error message
2017-11-06 23:14:04,731 - log3.py[line:30] - CRITICAL: 这是 logger critical message
3、日志格式说明
logging.basicConfig函数中,可以指定日志的输出格式format,这个参数可以输出很多有用的信息,如下:
- %(levelno)s: 打印日志级别的数值
- %(levelname)s: 打印日志级别名称
- %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
- %(filename)s: 打印当前执行程序名
- %(funcName)s: 打印日志的当前函数
- %(lineno)d: 打印日志的当前行号
- %(asctime)s: 打印日志的时间
- %(thread)d: 打印线程ID
- %(threadName)s: 打印线程名称
- %(process)d: 打印进程ID
- %(message)s: 打印日志信息
在工作中给的常用格式如下:
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
这个格式可以输出日志的打印时间,是哪个模块输出的,输出的日志级别是什么,以及输入的日志内容。
mini-web框架 添加log日志的更多相关文章
- [Python之路] 使用装饰器给Web框架添加路由功能(静态、动态、伪静态URL)
一.观察以下代码 以下来自 Python实现简易HTTP服务器与MINI WEB框架(利用WSGI实现服务器与框架解耦) 中的mini_frame最后版本的代码: import time def in ...
- scrapy框架之log日志
scrapy中的debug信息 在scrapy中设置log 1.在settings中设置log级别,在settings.py中添加一行: Scrapy提供5层logging级别: CRITICAL - ...
- [Python之路] 实现简易HTTP服务器与MINI WEB框架(利用WSGI实现服务器与框架解耦)
本文描述如果简单实现自定义Web服务器与自定义简易框架,并且不断进行版本迭代,从而清晰的展现服务器与Web框架之间是如何结合.如何配合工作的.以及WSGI是什么. 本文帖的代码有点多,但基本每次迭代修 ...
- 在idea中如何添加log日志
1.首先下载log4的jar包,官方路径为:http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.17/log4j-1.2.17.zip 2.下 ...
- 为App添加Log日志文件
using System; using System.Globalization; using System.IO; using System.Text; using System.Windows.F ...
- Spring框架之log日志的使用
1.Spring框架也需要引入日志相关的jar包 * 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.sprin ...
- 怎样在idea添加log日志 以及log4j2配置文件解读
网上找了很多篇文章,就数这篇比较全,从下载到配置都有讲到,解决从0开始接触java日志文件添加的各位同学.参考文章:https://www.cnblogs.com/hong-fithing/p/769 ...
- c++ 程序通用多线程单例设计 c++ web 框架设计经验谈
设计 c++ web 框架时候,想要一个框架缓存类,很多通用缓存类是用字符保存,作为框架内置就不要序列和反序列了,因为框架内部使用. 想给自己的paozhu c++ web 框架添加缓存类,参考了sp ...
- IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置
1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...
- 【原创】ASP.NET Web开发,实现打印Log日志,步骤详解
添加Log需要四步: 一.引用log4net.dll,详见附件:http://pan.baidu.com/s/1c0hab2g 二.项目根目录下,添加 log4net.xml <?xml ver ...
随机推荐
- 陆吾AI智能机械狗的通讯控制
陆吾AI智能机械狗现在是蛮有名的了,在YouTube上比较火的一个东西了,和波士顿机器狗不同,波士顿机器狗价格昂贵主要原因是其定位于工业领域的机械狗因此采用的是工业级的硬件,但是如果我们采用的家用环境 ...
- 9组-Alpha冲刺-6/6
一.基本情况 队名:不行就摆了吧 组长博客: https://www.cnblogs.com/Microsoft-hc/p/15546711.html 小组人数: 8 二.冲刺概况汇报 张伟鹏 过去两 ...
- 2024 年了,IT 运维监控系统都有哪些推荐?
大浪淘沙,2024 年的今天,市面上很多监控系统慢慢淡出了大家的视野,而一些新的监控系统也逐渐崭露头角.今天我们就来看看 2024 年的当下,哪些 IT 运维监控系统最值得关注. Prometheus ...
- dubbo服务治理(一)降级
在线网站一般都会有服务器压力剧增的时候,比如说网上商城的促销,这个时候常用的手段就是服务降级,根据当前业务情况及流量对一些服务和页面有策略的降级,以此缓解了服务器资源压力,以保证核心任务的正常运行,同 ...
- CentOS 7.3离线安装 JDK,Mariadb
1.环境准备 #链接:https://pan.baidu.com/s/1rcLrELaLHBJI0pKQQFOnNA #提取码:f1a2 2.安装 # 离线文件位置 /home/bw/# 将目标机器系 ...
- 后缀数组--SA--字符串
SA (Suffix Array) -- 后缀数组 简介 这里明白两个定义: \(SA_i\) : 按字典序排列后大小为 \(i\) 的后缀的后缀头的下标. \(Rank_i\) : 后缀头的下标为 ...
- js_for循环的错误
本段代码实现的效果是遍历数组中的每个元素,给每个元素插入一个类名 for (var i = 0; i < dropdownLi.length; i++) { if(i == 1){ contin ...
- 增删demo中,React开发中,Vue思维导致的踩坑
.push等操作,无法监听数据的更新,必须使用setState() state最好写在构造函数中,这是个好习惯 不要什么状态的获取都放在didmount,构造函数里面获取状态也是一个不错的选择
- Word在不同电脑排版异常
Word在不同电脑排版异常 问题描述 今天又有同学向我抱怨用 word 写的论文明明在自己的电脑格式调的好好的,怎么在导师那格式又乱了,害的挨批. 笔者也遇到过该问题,正好趁这次机会简单整理一下. 注 ...
- c# RSA加密解密,与java代码互通问题
RSA加密解密原本是公开算法,但是和一个java的小伙伴对接却出现了点问题,现在记录一下 首先,RSA的公钥私钥,有2种: 1.pem格式. 2.xml格式. 文章底部有pem格式和对应的xml样本数 ...