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 = """

%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.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 = """

%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)

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--->%sn'

mini mysql_mini相关推荐

  1. 正点原子:STM32F103(战舰)、STM32F407(探索者)、STM32F103(MINI)原理图和PCB

    目录 1.STM32F103(战舰) 2.STM32F407(探索者) 3.STM32F103(MINI) 为各位嵌入式好朋友分享三个重磅资源,正点原子三件套,可直接打样使用~ 1.STM32F103 ...

  2. 投影转换_即插即用,办公投影不用愁:毕亚兹Mini DP转HDMIVGA转换器

    日常办公的时候一些办公小件也很有用的,就比如说HDMI,VGA的转接头,不起眼但是很实用.去客户那里汇报工作,笔记本没有VGA接口,结果会很尴尬,到处借,没有转接头就是接不了,所以索性还是自己入手一个 ...

  3. 性能优化工具 MVC Mini Profiler

    MVC MiniProfiler是Stack Overflow团队设计的一款对ASP.NET MVC.WebForm 以及WCF 的性能分析的小程序.可以对一个页面本身,及该页面通过直接引用.Ajax ...

  4. iPad mini时隔四年更新,搭载A12芯片,起售价2999

    整理 | 非主流 出品 | AI科技大本营(公众号id:rgznai100) 距离苹果的春季发布还有一周,但就在昨天,苹果毫无征兆地给广大果粉来了一场预热. 3 月 18 日下午,苹果官网进行更新,悄 ...

  5. AI一分钟 | 小米发布小爱音箱mini,169元;天猫汽车无人贩卖机大楼落地,刷脸可购车试驾

    2018 区块链技术及应用峰会(BTA)·中国 倒计时 3 天 2018,想要follow最火的区块链技术?你还差一场严谨纯粹的技术交流会--2018区块链技术及应用峰会(BTA)·中国将于2018年 ...

  6. AI一分钟 | 小米智能音箱mini版曝光,或售199元;特朗普被指利用AI竞选成功

    整理 | 阿司匹林 一分钟AI 3月19日,小米社区有网友曝光了小爱同学mini版,它可能是3月27日小米MIX 2S发布会的"小惊喜",售价可能为199元. 据外媒报道,剑桥分析 ...

  7. 苹果12 Pro Max和mini测评来了,看完我选择了iPhone 12

    萧箫 发自 凹非寺 量子位 报道 | 公众号 QbitAI -iPhone 12 Pro Max和iPhone 12 mini,现在开售了. 不过这两款手机,媒体评价究竟如何? 来自The Verge ...

  8. 4.7 mini趴 走进猎豹

    2019独角兽企业重金招聘Python工程师标准>>> 4.7 mini趴 走进猎豹 技术分享提醒: 时间: 今晚 19:30~21:00 地点: 富力盈通31F 3110 会议室 ...

  9. TechParty Mini.0

    2019独角兽企业重金招聘Python工程师标准>>> 150310 Mini.0 ~ 任性,体验 原案: 150310 Mini.0 - techparty.hackpad.com ...

最新文章

  1. 华裔教授教你写论文2.引言的逻辑解析
  2. leetcode 349. 两个数组的交集 两种方案,c语言实现
  3. 吴恩达深度学习课程deeplearning.ai课程作业:Class 4 Week 2 Keras - Tutorial - Happy House
  4. 机器学习中的损失函数
  5. HashMap 源码解析(JDK1.8)
  6. Bugzilla集成LDAP的方法
  7. dedecms批量删除文档关键词可以吗
  8. oracle中那个日期怎么相减_oracle数据库中日期加减函数
  9. 双12压测引出的线上Full GC排查
  10. fastdfs暗转 linux_Linux下安装fastDFS
  11. tt c mysql t4 bll_通过T4模板实现代码自动生成
  12. SpringMVC + security模块 框架整合详解
  13. windows下的dig 命令和nslookup的用法及详例
  14. eclipse安装程序没反应
  15. 3.13 speculative generality (夸夸其谈未来性)
  16. Ed2k协议背景介绍及eMule协议的整体架构
  17. SAP License:反记账功能的应用
  18. linux环境下编译部署php生产环境
  19. 2019年中国国际信息通信展览会圆满闭幕
  20. 桌面级群控机器人CoCube探索-2022-

热门文章

  1. tomcat源码_从源码角度讲Tomcat在SpringBoot 中是如何启动的?
  2. Nature综述:多年冻土的微生物组
  3. Python使用matplotlib可视化条形图(bar plot)、自定义在条形图的顶部添加数值标签(Bar Chart)
  4. pandas使用read_csv读取文件数据、设置converters参数将百分比字符串转换为数字
  5. pandas使用query函数查询dataframe指定数据列的内容(数值)不包含在特定列表中的数据行(not contain in list)
  6. R语言构建xgboost模型并评估模型(测试集、训练集每一轮):误分类率指标(misclassification rate)、logloss
  7. R语言ggplot2时间序列可视化并在特定日期处添加竖线实战
  8. R语言dplyr包使用arrange函数、group_by函数、mutate函数生成分组数据的排名(rank)实战(Rank Variable by Group):升序排名、降序排名以及相同排名的处理
  9. 潜在狄利克雷分配(LDA,Latent Dirichlet Allocation)模型(二)
  10. R字符串(Strings)转为日期类型(Dates)