Debian Codegraph @

scripts/register

raw
1 #!/usr/bin/python3
2 """
3 request a unicast registration
4 """
5 import argparse
6 import json
7 import requests
8 import subprocess
9 import datetime
10 import sys
11
12
13 def main():
14 parser = argparse.ArgumentParser(description=__doc__)
15 parser.add_argument('deviceid', nargs=1)
16 parser.add_argument('appid', nargs=1)
17 parser.add_argument('-H', '--host',
18 help="host:port (default: %(default)s)",
19 default="localhost:8080")
20 parser.add_argument('--no-https', action='store_true', default=False)
21 parser.add_argument('--insecure', action='store_true', default=False,
22 help="don't check host/certs with https")
23 parser.add_argument('--unregister', action='store_true', default=False)
24 args = parser.parse_args()
25 scheme = 'https'
26 if args.no_https:
27 scheme = 'http'
28 if args.unregister:
29 op = 'unregister'
30 else:
31 op = 'register'
32 url = "%s://%s/%s" % (scheme, args.host, op)
33 body = {
34 'deviceid': args.deviceid[0],
35 'appid': args.appid[0],
36 }
37 headers = {'Content-Type': 'application/json'}
38 r = requests.post(url, data=json.dumps(body), headers=headers,
39 verify=not args.insecure)
40 if r.status_code == 200 and not args.unregister:
41 print(r.json()['token'])
42 else:
43 print(r.status_code)
44 print(r.text)
45 if r.status_code != 200:
46 sys.exit(1)
47
48 if __name__ == '__main__':
49 main()