在RaspberryPi上使用Flask Application获得500错误

时间:2015-08-12 16:25:37

标签: python flask python-requests

我已经构建了一个小型网页,该网页从阅读器中获取RFID ID并检查用户是否已经存在,如果是,则将其重定向到特定页面。在我的笔记本电脑上一切正常,但由于我已将其移至RPi,我得到了一个奇怪的500错误。

终端消息应该是什么样的:

starting
{'MemberID': u'7A0092B6F9A7'}
Member ID 7A0092B6F9A7
{"MemberExists":true,"MemberName":"WORK","State":"Logged In"}
True
Noooooo
127.0.0.1 - - [12/Aug/2015 10:52:00] "POST /index HTTP/1.1" 200 -
Sending confirmation for check in
127.0.0.1 - - [12/Aug/2015 10:52:00] "GET /getSignIn HTTP/1.1" 200 -
It worked
127.0.0.1 - - [12/Aug/2015 10:52:00] "GET /activity HTTP/1.1" 200 -

它实际上是什么样的:

starting
{'MemberID': u'7A0092B6F9A7'}
Member ID 7A0092B6F9A7
{"MemberExists":true,"MemberName":"WORK","State":"Logged Out"}
127.0.0.1 - - [12/Aug/2015 16:02:12] "POST /index HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/share/pyshared/flask/app.py", line 1518, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/share/pyshared/flask/app.py", line 1506, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/share/pyshared/flask/app.py", line 1504, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/share/pyshared/flask/app.py", line 1264, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/share/pyshared/flask/app.py", line 1262, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/share/pyshared/flask/app.py", line 1248, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/pi/connectedLogin/app/views.py", line 64, in index
    isMember = q.json()["MemberExists"]
TypeError: 'dict' object is not callable
Sending confirmation for check in
127.0.0.1 - - [12/Aug/2015 16:02:12] "GET /getSignIn HTTP/1.1" 200 -
It worked
127.0.0.1 - - [12/Aug/2015 16:02:12] "GET /signup HTTP/1.1" 200 -

这是我的Flask代码:

@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index(): 
    global checkCheck
    global userIdentifier
    print "starting"
    location = {'mspace': 'Central Library'} 
    if request.method == 'POST':
        global userIdentifier
        global isMember
        global memberID
        checkCheck = True
        member = {'MemberID': request.form['cardID']}
        memberID = request.form['cardID']
        print member
        print "Member ID " + memberID
        q = requests.post("https://external_DB/login.json", data=member)
        print q.text
        isMember = q.json()["MemberExists"]
        print isMember
        userIdentifier = q.json()["MemberName"]

        if isMember == False:
            #do stuff for a new member
            print "YEAH"
            print userIdentifier

        else:
            #do stuff for a recurring member
            print "Noooooo"

        return render_template('index.html',
                    location = location,
                    user = member
                    ) 

我的HTML:

<html>
    <head>
        <title>{{ location['mspace'] }} - Makerspace </title>
    </head>
    <body>
        <div id="banner">
            <img src="{{url_for('static', filename='bubbler.jpeg')}}" style="width:100%"></img>
        </div>
        <div style="text-align: center; display: table; width: 100%; height: 77%">
            <h1 style="display: table-cell; vertical-align: middle">Welcome to {{ location['mspace'] }} - Place your card on the reader to begin</h1>
        </div>
    </body>  
    <script>
    function httpGet(theUrl)
    {
               var xmlHttp = new XMLHttpRequest();
               xmlHttp.open( "GET", theUrl, false );
               xmlHttp.send(null);
               return xmlHttp.responseText;
    }

    var refreshIntervalId = setInterval(function(){checkFunc()}, 1000);

    function checkFunc(){
        var json = httpGet("/getSignIn");
            console.log("yes!");
            obj = JSON.parse(json);
            console.log("new checkin = "+obj.newCheckin);
            console.log("MemberID = "+obj.MemberID);
            console.log("IsMember? "+obj.isMember);
            if(obj.newCheckin){
                clearInterval(refreshIntervalId);
                checkUser(obj.isMember, obj.memberID);
            }
    }
    function checkUser(isMember, memberID){
        if(isMember == false){
            window.location = "signup";
        }
        else{
            window.location = "activity";
        }
    }
  </script>
</html>

2 个答案:

答案 0 :(得分:3)

您使用的是旧版requests。在版本1.0.0之前,response.json属性是属性直接返回JSON对象。如果您使用的是基于Debian或Ubuntu的Linux发行版,则可能安装了0.8.4版本。

requests库升级到较新版本(您的版本已过期近3年),或删除()来电:

isMember = q.json["MemberExists"]
userIdentifier = q.json["MemberName"]

答案 1 :(得分:1)

以下是错误消息的核心:

  File "/home/pi/connectedLogin/app/views.py", line 64, in index
    isMember = q.json()["MemberExists"]
TypeError: 'dict' object is not callable

这意味着q.json的价值是&#39; dict&#39;宾语。该行代码期望它是一个可以调用的方法。

我不熟悉烧瓶,但熟悉python。 q.json需要重新定义为方法,或者需要更改代码行,因此它不会调用它。例如isMember = q.json["MemberExists"]