什么导致Flask无法开始运行?

时间:2014-05-16 15:16:12

标签: python api flask raspberry-pi

我一段时间以来一直在研究一些代码,但是没有机会看了两个星期。我刚刚回到它,真的无法弄清楚它为什么不起作用。当我离开它时,我认为这是有效的(不完美,因为它还没有完成)但是我一直在为Flask本身收到错误,但只有这个代码。请有人看看,看看有什么明显的东西吗?

# add flask here
from flask import Flask
from flask import request
from flask import jsonify

app = Flask(__name__)
app.debug = True

# keep your code
import time
import cgi
import json

from tellcore.telldus import TelldusCore
core = TelldusCore()
devices = core.devices()

# define a "power ON api endpoint"
# START ENDPOINT DECLARATION

@app.route("/API/v1.0/power-on",methods=['POST'])

def powerOnDevice():

        payload = {}
        payload['success'] = False
        payload['message'] = "An unspecified error occured"
        #get the device by id somehow


        # get some extra parameters
        # let's say how long to stay on

        # PARAMS MUST BE HERE
        #params = request.json

        jsonData = request.get_json()
        print jsonData['deviceID']

        device = -1
        powerAction = "none"
        time = 0
        password = "none"

        #checks to make sure the deviceId has been specified and is a valid number
        try:
                device = devices[int(jsonData['deviceID'])]
        except:
                payload['message'] = "Incorrect deviceId specified"
                return jsonify(**payload)

        #checks to make sure the powerAction has been specified and is valid text
        try:
                powerAction = jsonData['powerAction']

                if (jsonData['powerAction'] == "on" or jsonData['powerAction'] == "off"):
                        powerAction = jsonData['powerAction']
        except:
                payload['message'] = "Incorrect powerAction specified"
                return jsonify(**payload)

        #check password is specified and is text
        try:
                password = jsonData['password']

                if (jsonData['password']

        #check time is number and is specified


        if (jsonData['pass'] == "support"):

                try:
                        device.turn_on()
                        payload['success'] = True
                        payload['message'] = ""
                        return jsonify(**payload)

                except:
                        payload['success'] = False
                        # add an exception description here
                        return jsonify(**payload)
        else:
                payload['message'] = "Incorrect password"
                # END ENDPOINT DECLARATION
                return jsonify(**payload)

# define a "power OFF api endpoint"
@app.route("/API/v1.0/power-off/<deviceId>",methods=['POST'])
def powerOffDevice(deviceId):
    payload = {}
    #get the device by id somehow
    device = devices[int(deviceId)]
    try:
      device.turn_off()
      payload['success'] = True
      return payload
    except:
      payload['success'] = False
      # add an exception description here
      return payload

app.run(host='0.0.0.0', port=81, debug=True)

跑步时,我明白了:

pi@FOR-PI-01 ~/FlaskTesting $ sudo python flaskao150514.py
  File "flaskao150514.py", line 71
    if (jsonData['pass'] == "support"):
                                      ^
SyntaxError: invalid syntax

然后删除整个if语句会给我底部的app.run错误。我知道那里可能存在Python错误,但为什么Flask没有运行?

1 个答案:

答案 0 :(得分:0)

在引发语法错误的行之前,你留下了一条不完整的行

if (jsonData['password']

因为那里没有右括号,Python正在查看以下行以查看表达式的结束位置。

请注意,Python 不需要这些括号;以下是有效的Python:

if jsonData['pass'] == "support":

在括号上轻松一下,您就可以减少出现此类错误的机会。