| 1 |
#!/usr/bin/python |
| 2 |
""" |
| 3 |
send broadcast to channel with payload data |
| 4 |
""" |
| 5 |
import argparse |
| 6 |
import json |
| 7 |
import requests |
| 8 |
import requests.auth |
| 9 |
import datetime |
| 10 |
import sys |
| 11 |
|
| 12 |
|
| 13 |
def main(): |
| 14 |
parser = argparse.ArgumentParser(description=__doc__) |
| 15 |
parser.add_argument('channel', nargs=1) |
| 16 |
parser.add_argument('data', nargs=1) |
| 17 |
parser.add_argument('-H', '--host', |
| 18 |
help="host:port (default: %(default)s)", |
| 19 |
default="localhost:8080") |
| 20 |
parser.add_argument('-e', '--expire', |
| 21 |
help="expire after the given amount of time, " |
| 22 |
"use 'd' suffix for days, 's' for seconds" |
| 23 |
" (default: %(default)s)", default="1d") |
| 24 |
parser.add_argument('--no-https', action='store_true', default=False) |
| 25 |
parser.add_argument('--insecure', action='store_true', default=False, |
| 26 |
help="don't check host/certs with https") |
| 27 |
parser.add_argument('-u', '--user', default="") |
| 28 |
parser.add_argument('-p', '--password', default="") |
| 29 |
args = parser.parse_args() |
| 30 |
expire_on = datetime.datetime.utcnow() |
| 31 |
ex = args.expire |
| 32 |
if ex.endswith('d'): |
| 33 |
delta = datetime.timedelta(days=int(ex[:-1])) |
| 34 |
elif ex.endswith('s'): |
| 35 |
delta = datetime.timedelta(seconds=int(ex[:-1])) |
| 36 |
else: |
| 37 |
print >>sys.stderr, "unknown --expire suffix:", ex |
| 38 |
sys.exit(1) |
| 39 |
expire_on += delta |
| 40 |
scheme = 'https' |
| 41 |
if args.no_https: |
| 42 |
scheme = 'http' |
| 43 |
url = "%s://%s/broadcast" % (scheme, args.host) |
| 44 |
body = { |
| 45 |
'channel': args.channel[0], |
| 46 |
'data': json.loads(args.data[0]), |
| 47 |
'expire_on': expire_on.replace(microsecond=0).isoformat()+"Z" |
| 48 |
} |
| 49 |
xauth = {} |
| 50 |
if args.user and args.password: |
| 51 |
xauth = {'auth': requests.auth.HTTPBasicAuth(args.user, args.password)} |
| 52 |
headers = {'Content-Type': 'application/json'} |
| 53 |
r = requests.post(url, data=json.dumps(body), headers=headers, |
| 54 |
verify=not args.insecure, **xauth) |
| 55 |
print r.status_code |
| 56 |
print r.text |
| 57 |
|
| 58 |
if __name__ == '__main__': |
| 59 |
main() |