# 配置 ./config/config.py ~~~ import os import socket from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker __all__ = ('SYSCONF', 'dBSession') def get_ip(): """获取本机的IP""" s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] finally: s.close() return ip SYSCONF = { 'app': { 'root_path': (os.path.dirname(os.path.dirname(os.path.abspath(__file__)))).replace('\\', '/'), # 整个项目的根路径 'app_path': (os.getcwd()+'/application').replace('\\', '/'), # APPLICATION的根路径 'app_host': get_ip(), # 后台接口的IP地址 'app_port': 8082, # 后台接口访问的端口 }, 'databases': { 'host': '127.0.0.1', # 服务器地址 'port': 3306, # 端口 'db': 'appserver', # 数据库名称 'username': 'root', # 用户名 'password': '123', # 密码 'prefix': 'yp_', # 表前缀 'charset': 'utf8', # 编码 } } # 创建数据库及连接 db_url = "mysql+pymysql://{username}:{password}@{host}:{port}/{db}?charset={charset}".\ format(username=SYSCONF.get('databases').get('username'), password=SYSCONF.get('databases').get('password'), host=SYSCONF.get('databases').get('host'), port=SYSCONF.get('databases').get('port'), db=SYSCONF.get('databases').get('db'), charset=SYSCONF.get('databases').get('charset')) engine = create_engine(db_url) # 创建DBSession类型: DBSession = sessionmaker(bind=engine) dBSession = DBSession() ~~~ ./config/route.py ~~~ from config import config from application.index import route as indexRoute def reg_routes(app): # 后台路由 indexRoute.reg_routes(app) ~~~