是什么导致Flask无法启动?

-1 投票
1 回答
697 浏览
提问于 2025-04-18 06:46

我最近在写一些代码,但已经有两周没碰它了。现在我回过头来看看,真的搞不懂为什么它不工作。我离开的时候觉得它还可以(虽然还没完成),但现在我在运行这段代码时总是遇到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

你在抛出语法错误的那一行之前,留了一行不完整的代码:

if (jsonData['password']

因为那里没有闭合的括号,Python会继续查看后面的行,试图找到这个表达式的结束位置。

要注意,Python其实不需要那些括号;下面这个也是有效的Python代码:

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

少用括号,这样你就能减少出现这种错误的机会。

撰写回答