Skip to content

Instantly share code, notes, and snippets.

@automactic
Created July 28, 2017 18:36
Show Gist options
  • Save automactic/778287df645bf8c9aaa1e0aa6e2ee268 to your computer and use it in GitHub Desktop.
Save automactic/778287df645bf8c9aaa1e0aa6e2ee268 to your computer and use it in GitHub Desktop.
python KSensor WebAPI request sample code
from datetime import datetime
import json
import urllib.request
def send_data(url: str, sid, pknnmb, timestamp: datetime, baseline, stddev, measures):
payload = {
'sid': str(sid),
'pknmb': str(pknnmb),
'timestamp': timestamp.strftime('%Y-%m-%d %H:%M:%S'),
'baseline': baseline,
'stddev': stddev,
'measures': measures,
}
headers = {'Content-Type': 'application/json; charset=utf-8'}
request = urllib.request.Request(url, json.dumps(payload).encode('utf-8'), headers)
with urllib.request.urlopen(request) as response:
json_response = json.loads(response.read().decode('utf-8'))
# print(json_response)
return json_response['success'] == 'true'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment