mock接口开发

故事扮演 提交于 2019-12-04 14:02:23
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)  启动服务
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!