Mercurial > lbo > hg > geohub
changeset 105:44b0d141fcea
track_ICE: Use requests session for keepalive
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Fri, 25 Dec 2020 11:58:49 +0100 |
parents | ea32bfe4de7f |
children | da9fb0032788 |
files | examples/track_ICE/collect.py |
diffstat | 1 files changed, 5 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/track_ICE/collect.py Fri Dec 25 10:32:40 2020 +0100 +++ b/examples/track_ICE/collect.py Fri Dec 25 11:58:49 2020 +0100 @@ -18,7 +18,7 @@ def format_server_time(servertime): return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(servertime/1000)) -def send_point(args, info: dict[str, str]): +def send_point(sess, args, info: dict[str, str]): geohub_templ = args.geohub + '/{CLIENT}/log?secret={SECRET}' geohub_url = geohub_templ.format(HOST=args.geohub_host, CLIENT=args.client or info.get('tzn', 'TRAIN'), SECRET=args.secret, PROTOCOL=args.geohub_scheme) additional = '&lat={lat}&longitude={long}&s={spd}&time={ts}'.format( @@ -27,7 +27,7 @@ for k in ['latitude', 'longitude', 'speed', 'serverTime']: info.pop(k) url = geohub_url + additional - requests.post(url, json=info) + sess.post(url, json=info) def parse_args(): parser = argparse.ArgumentParser(description='Fetch and send train data') @@ -52,11 +52,13 @@ eprint('Running in train:', tzn) eprint('Go to LiveMap:', livemap_url); + session = requests.Session() + with open(args.outfile, 'w') as outfile: while True: if info: eprint('{} :: Sending point ({}, {}) to GeoHub.'.format(format_server_time(info['serverTime']), info['longitude'], info['latitude'])) - send_point(args, info) + send_point(session, args, info) outfile.write(json.dumps(info)) outfile.write('\n') else: