我已经问过这个问题了,但我认为我问的方式不对,或者至少我可能会在问题所在的地方吠叫错误的树。
我有一个带有以下路线的 Python/Flask 脚本:
@app.route('/heatingadjust')
def heatingadjust(hiveSessionId=None, score=None):
import requests
import time
import datetime
import MySQLdb
conn = MySQLdb.connect(host="localhost", user = "admin", passwd = "xxxxxxxxxx", db = "mydb")
cursor = conn.cursor()
cursor.execute("select score from OccScore")
data = cursor.fetchone()
score = data[0]
url = "https://api.prod.bgchprod.info:443/omnia/users"
if 'hiveSessionId' in session:
hiveSessionId = session['hiveSessionId']
headers = {
'Content-Type': "application/vnd.alertme.zoo-6.1+json",
'Accept': "application/vnd.alertme.zoo-6.1+json",
'X-Omnia-Client': "Hive Web Dashboard",
'X-Omnia-Access-Token': hiveSessionId,
'Cache-Control': "no-cache"
}
response = requests.request("GET", url, headers=headers)
data=response.json()
if 'errors' in data:
return redirect(url_for('hivelogin'))
if (score == 0):
url = "https://api-prod.bgchprod.info:443/omnia/nodes/xxxxxxxxxx"
payload = "{\n \"nodes\": [{\n \"attributes\": {\n \"targetHeatTemperature\": {\n \"targetValue\": 15\n }\n }\n }]\n}"
headers = {
'Content-Type': "application/vnd.alertme.zoo-6.1+json",
'Accept': "application/vnd.alertme.zoo-6.1+json",
'X-Omnia-Client': "Dashboard",
'X-Omnia-Access-token': hiveSessionId,
'Cache-Control': "no-cache",
}
response = requests.request("PUT", url, data=payload, headers=headers)
else:
url = "https://api-prod.bgchprod.info:443/omnia/nodes/xxxxxxx"
payload = "{\n \"nodes\": [{\n \"attributes\": {\n \"targetHeatTemperature\": {\n \"targetValue\": 18\n }\n }\n }]\n}"
headers = {
'Content-Type': "application/vnd.alertme.zoo-6.1+json",
'Accept': "application/vnd.alertme.zoo-6.1+json",
'X-Omnia-Client': "Dashboard",
'X-Omnia-Access-token': hiveSessionId,
'Cache-Control': "no-cache",
}
response = requests.request("PUT", url, data=payload, headers=headers)
return str(score)
基本上,我有一个使用 wget 调用的路由,它会嗅探蓝牙设备,如果找到设备并将分数写入 MySQL 表,则作为分数递增。这是看家里有没有人。
如果房子是空的,这条路线会读取分数并使用 Hive api 关闭加热。从 URL 调用,它完全按照它应该做的,我可以看到加热被关闭。
但是,我想要做的是使用 crontab 中的 wget 调用脚本。
crontab 运行,我可以在 syslog 中看到它。apache 访问日志显示正在调用的 URL。
但是加热不会像我通过浏览器调用完全相同的东西那样关闭。
我从我看到的其他方面怀疑这可能是因为 wget 不喜欢调用另一个 URL 的事实。
谁能告诉我我的怀疑是否正确?我已经看到了关于使用 urllib 而不是 wget 的其他内容,但我不知道我需要更改什么才能使用标头调用 URL。
我的猜测是该wget
请求不提供会话 cookie,因此hiveSessionId
在session
. 因为那时'X-Omnia-Access-token': None
对 Omnia 服务的请求被拒绝了。
(我对这两个参数有点困惑,hiveSessionId
和score
。如果 hive 会话 ID 存在,它会被会话中的值覆盖,并且分数总是被数据库中的值覆盖。)
旁注:您可以使用json.dumps()
来构建有效负载:
payload = json.dumps({
"nodes": [
{
"attributes": {"targetHeatTemperature": {"targetValue": 18}}
}
]
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句