Python-mini-web框架

web-框架

from pymysql import *
import re

# 定义空字典
URL_DICT = dict()
# 准备装饰器
def route(url):
	def set_func(func):
		URL_DICT[url] = func
		def call_func(*args,**kwargs):
			return func(*args,**kwargs)
		return call_func
	return set_func

@route('/index.py')
def index():
	with open('/templatss/index.html') as f:
		content = f.read()
	# 创建connect连接
	conn = connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
	# 创建cursor游标对象
	cs = conn.cursor()
	# sql语句
	sql = ''
	cs.execute(sql)
	tuples = cs.fetchall()
	cs.close()
	conn.close()
	url_templte = '''

'''
	html = ''
	for info in tuples:
		html += url_templte % (info[0],info[1])
	content = re.sub(r"\{%content%\}",html,content)
	return content

@route('/center.py')
def center():
	with open ('/templatess/center.html') as f:
		content = f.read()
	# 创建connect连接
	conn = connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
	# 创建cursor游标对象
	cs = conn.cursor()
	# sql 语句
	sql = ''
	cs.execute(sql)
	# 获取数据并返回一个元组
	tuples = cs.fetchall()
	# 关闭游标对象
	cs.close()
	# 关闭连接对象
	conn.close()
	url_template = '''

'''
	html = ''
	for info in tuples:
		html += url_template % (info[0],info[1]......)
	content = re.sub(r"\{%content%\}",html,content)
	return content

def application(env,server_frame):
	status = '200 OK'
	response_header = [('Content-Type','text/html;charset=utf8')]
	file_name = env['PATH_INTO']
	
	try:
		func = URL_DICT[url]
		return func()
	except Exception as ret:
		retun '产生异常,%s' % str(ret)	
	

你可能感兴趣的:(Python-mini-web框架)