【问题标题】:What is causing Flask to not start running? [closed]是什么导致 Flask 无法开始运行? [关闭]
【发布时间】:2014-05-16 15:16:11
【问题描述】:

我一直在编写一些代码,但已经有两个星期没有机会查看它了。我刚刚回到它,真的无法弄清楚为什么它不起作用。当我离开它时,我认为它正在工作(不完美,因为它还没有完成)但我不断收到 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 没有运行?

【问题讨论】:

  • 查看上面的代码行。它不完整。 (而且,一般来说,如果您无法在报告的行中发现任何语言的语法错误,请检查前一行是否有未闭合的括号或其他缺少的标点符号。)
  • 我现在觉得自己很蠢!感谢您的提示,当我将来看到错误时,我会记住它。

标签: python api flask raspberry-pi


【解决方案1】:

您在引发语法错误的行之前留下了不完整的行:

if (jsonData['password']

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

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

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

在括号上放轻松,这样错误的机会就会减少。

【讨论】:

  • 我现在觉得自己很蠢哈哈!谢谢,我不知道。认为我应该回到 Codecademy 并再次学习基础知识。
猜你喜欢
  • 1970-01-01
  • 2021-10-08
  • 2020-03-12
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多