forked from interaktivarum/nfc2server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
33 lines (26 loc) · 1.21 KB
/
functions.py
File metadata and controls
33 lines (26 loc) · 1.21 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
import binascii, requests, json, datetime
def sendRequest(ip,host,endpoint,static_user_data,uid,action):
data = {'staticUserData':static_user_data, 'action':action, 'tagUid':binascii.hexlify(uid), 'time':datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')}
print('\nSend server request to: ' + ip + " " + host)
url = ''
headers = {'Content-Type': 'application/json', 'X-TagUID':binascii.hexlify(uid)}
r = None
if ip != '':
url = 'http://' + ip + '/' + endpoint
headers = {'host': host, 'Content-Type': 'application/json', 'X-TagUID':binascii.hexlify(uid)}
else:
url = 'http://' + host + '/' + endpoint
#Post request with host name
try:
r = requests.post(url, data=json.dumps(data), headers=headers, timeout=3)
print(r)
except requests.exceptions.ConnectionError:
print "Network connection error"
except requests.exceptions.Timeout:
print "Timeout exception"
if r != None:
if r.status_code == 200:
print('Server response:\n' + r.content)
else:
print 'REQUEST ERROR! Status code: ' + str(r.status_code)
print('Server response:\n' + r.content)