需求:易维护,可扩展
所需依赖
appdirs==1.4.3
futures==3.1.1
netaddr==0.7.19
packaging==16.8
pbkdf2==1.3
Pillow==4.1.0
pkg-resources==0.0.0
pycket==0.3.0
PyMySQL==0.7.11
pyparsing==2.2.0
qiniu==7.1.2
redis==2.10.5
requests==2.14.2
SQLAlchemy==1.1.9
tornado==4.4.3
torndb==0.3
#coding=utf-8
settings = dict(
template_path='templates', #设置模板路径
static_path='static', #设置静态文件路径
debug=True, #调试模式
cookie_secret='aaaa', #cookie加密方式
login_url='/auth/user_login', #auth 指定默认的路径
xsrf_cookies=True, #防止跨域攻击
#ui_methods=admin_uimethods,
#pycket配置信息
pycket={
'engine': 'redis',
'storage': {
'host': 'localhost',
'port': 6379,
'db_sessions': 5,
'db_notifications': 11,
'max_connections': 2 ** 31,
},
'cookies': {
'expires_days':30, #设置过期时间
#'max_age':5000,
}
}
)
#coding=utf-8
import tornado.httpserver
import tornado.web
import tornado.ioloop
import tornado.escape
from tornado.options import define, options
from config import settings
from handlers.main.main_urls import handlers
#定义一个默认的接口
define("port", default=8000, help="run port",type=int)
define("start", default=False, help="start server", type=bool)
if __name__ == "__main__":
options.parse_command_line()
if options.start:
app = tornado.web.Application(handlers, **settings) #创建应用实例
http_server = tornado.httpserver.HTTPServer(app) #通过应用实例创建服务器实例
http_server.listen(options.port) #监听端口
print 'start server...'
tornado.ioloop.IOLoop.instance().start() #启动服务
#coding=utf-8
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# 连接数据库的数据
HOSTNAME = '192.168.169.138'
PORT = '3306'
DATABASE = 'tornado_pro'
USERNAME = 'developer'
PASSWORD = '123456'
# DB_URI的格式:dialect(mysql/sqlite)+driver://username:password@host:port/database?charset=utf8
DB_URI = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(USERNAME,
PASSWORD,
HOSTNAME,
PORT,
DATABASE
)
# 1创建一个engine引擎
engine = create_engine(DB_URI, echo=False )
# 2sessionmaker生成一个session类
Session = sessionmaker(bind=engine)
# 3创建一个session实例
dbSession = Session()
# 4创建一个模型基类
Base = declarative_base(engine)
#coding=utf-8
import redis
conn = redis.Redis()
1、继承tornado.web.RequestHandler, SessionMixin
2、创建数据库和redis的连接实例
3、重写方法get_current_user(self) 如果要使用authenticated方法
4、清除连接实例中的数据
#coding=utf-8
import tornado.escape
from libs.pycket.session import SessionMixin
from libs.db.dbsession import dbSession
from libs.redis_conn.redis_conn import conn
from models.account.account_user_model import User
users = {
'user': User
}
class BaseHandler(tornado.web.RequestHandler, SessionMixin):
def initialize(self): #初始化连接 db为mysql的数据库连接实例 conn为redis的连接实例
self.db=dbSession
self.conn=conn
def get_current_user(self): #重写方法 如果要使用authenticated方法
"""获取当前用户"""
username = self.session.get("user_name")
if username:
user = users[username['user_tablename']].by_id(username['user_id'])
return user if user else None
else:
return None
def on_finish(self): #清除连接中的数据
self.db.close()
#coding=utf-8
import tornado.web
from handlers.base.base_handler import BaseHandler
class MainHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
self.render('index.html')
7、编写main_handler_url
from main_handler import MainHandler
handlers = [
(r'/',MainHandler),
]