Python 带有 wget 和外部的脚本 api
Python script with wget and external api
我已经问过这个问题了,但我想我问的方式不对,或者至少我可能找错了问题所在。
我有一个 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 table,则分数会增加。这是看家里有没有人
如果房子是空的,这条路线会读取分数并使用 Hive api 关闭暖气。从 URL 调用,它完全按照它应该做的,我可以看到暖气被关闭。
但是,我想做的是使用 crontab 中的 wget 调用脚本。
crontab 运行,我可以在 syslog 中看到它。 apache 访问日志显示正在调用 URL。
但是暖气不会像我通过浏览器调用完全相同的东西那样关闭。
我从我看到的其他事情中怀疑这可能是因为 wget 不喜欢另一个 URL 被调用的事实。
谁能告诉我我的猜测是否正确?我已经看到了关于使用 urllib 而不是 wget 的其他内容,但我不知道我需要更改什么才能使用 headers.
调用 URL
我的猜测是 wget
请求没有提供会话 cookie,因此 session
中没有 hiveSessionId
。因为那时 'X-Omnia-Access-token': None
对 Omnia 服务的请求被拒绝了。
(我对 hiveSessionId
和 score
这两个参数有点困惑。如果会话存在,配置单元会话 ID 会被会话中的值覆盖,并且分数是 总是 被数据库中的值覆盖。)
旁注:您可以使用 json.dumps()
构建有效负载:
payload = json.dumps({
"nodes": [
{
"attributes": {"targetHeatTemperature": {"targetValue": 18}}
}
]
})
我已经问过这个问题了,但我想我问的方式不对,或者至少我可能找错了问题所在。
我有一个 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 table,则分数会增加。这是看家里有没有人
如果房子是空的,这条路线会读取分数并使用 Hive api 关闭暖气。从 URL 调用,它完全按照它应该做的,我可以看到暖气被关闭。
但是,我想做的是使用 crontab 中的 wget 调用脚本。
crontab 运行,我可以在 syslog 中看到它。 apache 访问日志显示正在调用 URL。
但是暖气不会像我通过浏览器调用完全相同的东西那样关闭。
我从我看到的其他事情中怀疑这可能是因为 wget 不喜欢另一个 URL 被调用的事实。
谁能告诉我我的猜测是否正确?我已经看到了关于使用 urllib 而不是 wget 的其他内容,但我不知道我需要更改什么才能使用 headers.
调用 URL我的猜测是 wget
请求没有提供会话 cookie,因此 session
中没有 hiveSessionId
。因为那时 'X-Omnia-Access-token': None
对 Omnia 服务的请求被拒绝了。
(我对 hiveSessionId
和 score
这两个参数有点困惑。如果会话存在,配置单元会话 ID 会被会话中的值覆盖,并且分数是 总是 被数据库中的值覆盖。)
旁注:您可以使用 json.dumps()
构建有效负载:
payload = json.dumps({
"nodes": [
{
"attributes": {"targetHeatTemperature": {"targetValue": 18}}
}
]
})