The submitting example made more comprehensible and reusable.

pull/1/head
Stanislaw Klekot 2014-06-29 14:13:35 +02:00
rodzic edc3597a43
commit c51d4c8f0e
1 zmienionych plików z 43 dodań i 18 usunięć

Wyświetl plik

@ -2,12 +2,49 @@
# #
# An example of how to submit data about simple sensor. # An example of how to submit data about simple sensor.
# #
#-----------------------------------------------------------------------------
import sys import sys
import httplib import httplib
import urlparse import urlparse
import json import json
#-----------------------------------------------------------------------------
class WLokalu:
def __init__(self, url):
urlp = urlparse.urlparse(url)
self.host = urlp.hostname
self.port = urlp.port
self.app_path = urlp.path.rstrip('/')
def request(self, method, path, body = None):
h = httplib.HTTPConnection(host = self.host, port = self.port)
if body is not None:
h.request(method, path, json.dumps(body))
else:
h.request(method, path)
resp = h.getresponse()
if resp.status / 100 == 2:
return json.loads(resp.read())
else:
raise Exception('HTTP error %d' % (resp.status,))
def get(self, sensor):
path = '%s/api/v1/sensor/%s' % (self.app_path, sensor)
return self.request('GET', path)
def put(self, sensor, state):
path = '%s/api/v1/sensor/%s' % (self.app_path, sensor)
return self.request('POST', path, {"state": state})
def delete(self, sensor):
path = '%s/api/v1/sensor/%s' % (self.app_path, sensor)
return self.request('DELETE', path)
#-----------------------------------------------------------------------------
if len(sys.argv) < 3 or sys.argv[1] in ('-h', '--help'): if len(sys.argv) < 3 or sys.argv[1] in ('-h', '--help'):
print "Usage:" print "Usage:"
print " %s http://wlokalu.hswro.org/ sensor-name state" % (sys.argv[0]) print " %s http://wlokalu.hswro.org/ sensor-name state" % (sys.argv[0])
@ -15,28 +52,16 @@ if len(sys.argv) < 3 or sys.argv[1] in ('-h', '--help'):
sys.exit() sys.exit()
if len(sys.argv) == 3: if len(sys.argv) == 3:
url = urlparse.urlparse(sys.argv[1]) url = sys.argv[1]
sensor = sys.argv[2] sensor = sys.argv[2]
state = None state = None
else: else:
url = urlparse.urlparse(sys.argv[1]) url = sys.argv[1]
sensor = sys.argv[2] sensor = sys.argv[2]
state = sys.argv[3] state = sys.argv[3]
h = httplib.HTTPConnection(host = url.hostname, port = url.port) wlokalu = WLokalu(url)
if url.path.endswith('/'): if state is not None:
path = '%sapi/v1/sensor/%s' % (url.path, sensor) wlokalu.put(sensor, state)
else: else:
path = '%s/api/v1/sensor/%s' % (url.path, sensor) wlokalu.delete(sensor)
if state is None:
h.request('DELETE', path)
else:
h.request('POST', path, json.dumps({"state": state}))
resp = h.getresponse()
if resp.status / 100 == 2:
print "OK"
print resp.read().strip()
else:
print "failed"