-
Notifications
You must be signed in to change notification settings - Fork 0
/
urest.py
33 lines (27 loc) · 796 Bytes
/
urest.py
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
import socket
import ujson
from uhttp.client import HTTPConnection
def get(url):
conn = HTTPConnection(url)
conn.request("GET", "/")
resp = conn.getresponse()
if resp.status == 200:
resp_json = ujson.loads(resp.read())
else:
# handle error...? Raise something?
print("Error while getting {}!".format(url))
print(resp)
print(resp.read())
def post(url, data):
conn = HTTPConnection(url)
conn.request("POST", "/", body=ujson.dumps(data))
resp = conn.getresponse()
if resp.status == 204:
return
elif resp.status == 200:
return ujson.loads(resp.read())
else:
# handle error...? Raise something?
print("Error while posting data to {}!".format(url))
print(resp)
print(resp.read())