mini框架

import re import time from pymysql import * import urllib.parse

全局变量存储请求文件的根路径信息

g_templates_root = "./templates" g_url_funcs = dict()

def function1(url):

def function2(func):
    g_url_funcs[url] = func

    def function3(file_name, *args):

        return None

    return None
return function2

@function1(r"/index.html") def index(file_name, url=None): try: file_name = file_name.replace(".py", ".html") file_html = open(g_templates_root + file_name) except Exception as ret: return "%s" % ret else: content = file_html.read() file_html.close() # 创建Connection连接 db = connect(host='localhost',port=3306,database='stock_db',user='root',password='mysql',charset='utf8') # 获得Cursor对象 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

@function1(r"/center.html") def center(file_name, url=None): try: file_name = file_name.replace(".py", ".html") file_html = open(g_templates_root + file_name) except Exception as ret: return "%s" % ret else: content = file_html.read() file_html.close()

    # 打开数据库
    db = 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

@function1(r"/update/(\d*).html") def update(file_name, url=None): """更新页面""" # url---> #update/300268.html ret = re.match(url, file_name) if ret: stock_code = ret.group(1) # return stock_code # 测试

try:

    file_html = open(g_templates_root + "/update.html")
except Exception as ret:
    return "%s" % ret
else:
    content = file_html.read()
    file_html.close()

    # 打开数据库
    db = connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
    cursor = db.cursor()
    sql = """select f.note_info from focus as f inner join info as i on f.info_id=i.id where i.code="%s";""" % (stock_code)

    cursor.execute(sql)
    data_from_mysql = cursor.fetchone()
    cursor.close()
    db.close()

    note_info = data_from_mysql[0]

    content = re.sub(r"\{%code%\}", stock_code, content)
    content = re.sub(r"\{%note_info%\}", note_info, content)

    return content

@function1(r"/update/(\d*)/(.*).html") def update_note_info(file_name, url=None): """更新页面""" # url---> #update/300268.html ret = re.match(url, file_name) if ret: stock_code = ret.group(1) stock_note_info = ret.group(2) stock_note_info = urllib.parse.unquote(stock_note_info)

# return stock_code  # 测试
# return str((stock_code, stock_note_info))


# 打开数据库
db = connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
cursor = db.cursor()
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 "修改成功"

@function1(r"/del/(\d*).html") def delete(file_name, url=None): """用来显示修改页面""" stock_code = ""

ret = re.match(url, file_name)
if ret:
    stock_code = ret.group(1)


# 创建Connection连接
db = connect(host='localhost',port=3306,database='stock_db',user='root',password='mysql',charset='utf8')
# 获得Cursor对象
cursor = db.cursor()
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 """取消关注成功"""

@function1(r"/add/(\d*).html") def add(file_name, url=None): """用来显示修改页面""" stock_code = ""

ret = re.match(url, file_name)
if ret:
    stock_code = ret.group(1)


# 创建Connection连接
db = connect(host='localhost',port=3306,database='stock_db',user='root',password='mysql',charset='utf8')
# 获得Cursor对象
cursor = db.cursor()

sql = """select * from focus where info_id = (select id from info where 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' # header 信息 response_headers = [('Content-Type', 'text/html')]

file_name = environ["PATH_INFO"]

# print("-"*30)
# 调用服务器的函数,给服务器传递参数 报头信息 
start_response(status, response_headers)

# 定义一个全局变量的字典,key=file_name values=函数的引用
# {"/index.html":index}
#update/300268.html
#r"/upate/\d*\.html":函数的引用
#
try:
    print("1")
    for url, call_func in g_url_funcs.items():
        print("2")
        ret = re.match(url, file_name)
        if ret:
            return call_func(file_name, url)

    else:
        print(g_url_funcs)
        return "sorry, 没有要找的页面 file_name(%s)" % (file_name)

except Exception as ret:
    print(g_url_funcs)
    return "cuowu"

转载于:https://my.oschina.net/u/3621947/blog/1518201

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值