程序分目录管理
szz_aip:工程目录
bin:可执行文件
start.py
config:配置文件
setting.py
lib:工具类、初始化服务类、接口类
interface.py 接口
tools.py 工具
logs:日志
readme.txt:说明
setting.py
#mysql数据库信息 mysql_info = { 'host': '127.0.0.1', 'port': 3306, 'password': '123456', 'user': 'xxx', 'db': 'xxx', 'charset': 'utf8', 'autocommit': True } #redis数据库信息 redis_info = { 'host': '127.0.0.1', #ip 'port': 6379, #端口号 'password': 'xxxx&*', #密码, 'db':0 } #falsk服务 server_info = { 'host':'0.0.0.0', 'port':8888, 'debug':True } #盐 SALT = '#$%^%^^326237'
tools.py
import hashlib,pymysql,flask from config import setting def md5(s): s = (str(s)+setting.SALT).encode() m = hashlib.md5(s) return m.hexdigest() #数据库操作 def op_mysql(sql:str): result = '执行完成' conn = pymysql.connect(**setting.mysql_info) cur = conn.cursor(pymysql.cursors.DictCursor) #建立游标 cur.execute(sql) if sql.strip().lower().startswith('select'): # result = cur.fetchone() result = cur.fetchall() cur.close() conn.close() return result
interface.py
import json,flask from . import tools #当前目录用. server =flask.Flask(__name__) @server.route('/table_data') def get_table_data(): table_name = flask.request.args.get('table_name') limit = flask.request.args.get('limit','10') tables = ['app_myuser','dsk_test','app_student','app_product'] if table_name not in tables: return json.dumps({'msg':'没有权限!'},ensure_ascii=False) if not table_name: return json.dumps({'msg':'table_name是必填字段!'}) if limit.isdigit(): sql = 'select * from %s limit %s; '%(table_name,limit) else: return json.dumps({'msg':'limit请传入一个整数!'},ensure_ascii=False) result = tools.op_mysql(sql) return json.dumps(result,ensure_ascii=False)
start.py
import sys import os #动态获取工程目录 # os.path.abspath((__file__) 获取当前文件的绝对路径 BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath((__file__)))) sys.path.insert(0,BASE_PATH) from config.setting import server_info from lib.interface import server server.run(**server_info)
需要设置目录 Sources Root,把整个目录加到环境变量里面去,才能导入这些python文件