import pymysql,hashlib,redis,flask,json,time
# 连接mysql和redis
r = redis.Redis(host='118.24.3.40', password='HK139bc&*', port=6379, db=6, decode_responses=True)
db_info = {'user': 'jxz', 'password': '123456','host': '118.24.3.40', 'db': 'jxz', 'port': 3306, 'charset': 'utf8','autocommit': True}
conn = pymysql.connect(**db_info)
cur = conn.cursor(pymysql.cursors.DictCursor)
server = flask.Flask(__name__) 本地启动服务
@server.route('/api/login') 开发一个登录接口
def login():
username = flask.request.values.get('username')
password = flask.request.values.get('password')
new_password = hashlib.md5(password.encode()).hexdigest()
cur.execute('select * from wyjregister where username = \'%s\''% username)
result = cur.fetchall()
# 校验用户和密码是否正确
if username == result[0].get('username') and new_password == result[0].get('password'):
sessionid = hashlib.md5((str(time.time()) + username).encode()).hexdigest()
r.set('sessiondid', '%s' % sessionid, 6000)
data = {'error_code': 1, 'msg': '登录成功','sessionid':sessionid}
else:
data = {'error_code': 0, 'msg': '登录失败'}
return json.dumps(data, ensure_ascii=False)
server.run(host='0.0.0.0',port=8000,debug=True) 启动服务