Python 是什么导致烧瓶无法开始运行?
我已经在编写一些代码有一段时间了,但已经两周没有机会看它了。我刚刚回到它,真的不明白它为什么不起作用。当我离开它时,我认为它可以工作(不是很完美,因为它还没有完成),但我一直在为Flask本身获取错误,但仅使用此代码。请有人看看有没有什么明显的东西Python 是什么导致烧瓶无法开始运行?,python,api,flask,raspberry-pi,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
# 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)
然后删除整个if语句会导致底部的app.run出现错误。我知道其中可能有Python错误,但为什么Flask没有运行?您在抛出语法错误的行之前留下了一行不完整的内容:
if (jsonData['password']
因为这里没有右括号,Python正在查看下面的行以查看表达式的结尾
注意Python不需要这些括号;以下是有效的Python:
if jsonData['pass'] == "support":
在括号中放轻松,这样会减少出现此类错误的机会。请看上面的代码行。这是不完整的。(而且,一般来说,如果你不能在任何语言的报告行中发现语法错误,请检查前一行是否有未闭合的括号或其他缺少的标点符号。)我现在觉得自己很愚蠢!谢谢你的提示,当我看到未来的错误时,我会牢记在心。我现在觉得自己很愚蠢,哈哈!谢谢,我不知道。我想我应该回到Codecademy,再次学习基础知识。
if jsonData['pass'] == "support":