Python requests.post在同一个Flask app中调用自己的api函数

时间:2015-12-17 21:10:48

标签: python flask

我的代码正在尝试检查登录用户名和密码,如果匹配,只需向localhost / api / homepage发送一个带有数据“values”的新帖子请求,我在python脚本的本地虚拟环境中尝试了它但是对于生产模式,在输入用户名和密码后,会发生502 Bad Gateways。我已经导入'requests'library.in Nginx设置使用了无负载均衡。

@app.route('/api/login',methods=['GET', 'POST'])
def login():
    username = request.form['usermail']
    pw = request.form['password']
    db=MySQLdb.connect("localhost","root","1234","4080")
    cursor = db.cursor()
    query = "select * from user where username='"+username+"' AND password='"+pw+"';"
    try:
        cursor.execute(query)
        result = cursor.fetchall()
    except:
        db.rollback()
        return query

   if len(result)==1:

       values ={'user':'wes'}
       r = requests.post('http://127.0.0.1/api/homepage' ,data=values)
       return  r.content

   else:
       return 'Fail login'

@app.route('/api/homepage',methods=['POST'])
def Display_Homepage():
    return request.form['user'];

0 个答案:

没有答案