forked from mar5chi/hand_gesture_control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiface.py
More file actions
53 lines (47 loc) · 1.92 KB
/
Copy pathiface.py
File metadata and controls
53 lines (47 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import requests
from furl import furl
from HgcException import HgcException
url_string = 'http://10.5.55.3:8080/rest/items'
test_url_string = 'http://192.168.43.142:8080/rest/items'
test_url_string2 = 'http://192.168.43.62:8080/rest/items'
url_str = test_url_string2
def get_state(item_name):
item_url = furl(url_str)
item_url.path = item_url.path / item_name / 'state'
try:
r = requests.get(item_url.url, timeout=5)
print(f'GET request status_code: {r.status_code}')
print(f'GET request text: {r.text}')
print(f'GET request reason: {r.reason}')
if r.status_code == 200: # 200 OK
return r.text
else:
return f'Error {r.status_code}: {r.reason}'
except requests.ConnectTimeout as ct:
print('get_state() ConnectTimeout: raising HgcException')
raise HgcException("Connection time out.")
except requests.exceptions.RequestException as re:
print("get_state() Error: ", re)
raise HgcException("No connection.")
def post_state(item_name, state):
item_url = furl(url_str)
# Add item name to url:
item_url.path = item_url.path / item_name
headers = {'Content-type': 'text/plain'}
try:
r = requests.post(item_url.url, state, headers=headers, timeout=5)
# TODO: add 404 Not Found, etc.
print(f'POST request status_code: {r.status_code}')
print(f'POST request text: {r.text}')
print(f'POST request reason: {r.reason}')
if r.status_code == 200: # 200 OK
return 'OK'
except requests.ConnectTimeout as ct:
print('raising HgcException')
raise HgcException("Connection time out.")
except requests.ConnectionError as e:
print('raising HgcException')
raise HgcException("Connection error.")
except requests.exceptions.RequestException as re:
print("Error: ", re)
raise HgcException("No connection.")