Python Application Tenmplate

JS
S
JavaScript

Simple application template for Python

1#!/usr/bin/python
2
3import time, requests, os
4from flask import Flask
5app = Flask(__name__)
6
7START = time.time()
8
9def elapsed():
10    running = time.time() - START
11    minutes, seconds = divmod(running, 60)
12    hours, minutes = divmod(minutes, 60)
13    return "%d:%02d:%02d" % (hours, minutes, seconds)
14
15def update():
16    try:
17        file = open("uuid.txt","r")
18        cid = file.read()
19    except:
20        cid = '100'
21
22    if "KUBERNETES_SERVICE_HOST" in os.environ:
23        ea = 'step5'
24    else:
25        ea = 'step2'
26
27    r = requests.post('http://www.google-analytics.com/collect',
28                      data = {'v': 1,
29                              'tid': 'UA-57322503-11',
30                              'cid': cid,
31                              't': 'event',
32                              'ec': 'tutorial',
33                              'ea': ea
34                              })
35
36@app.route('/')
37def root():
38    update()
39    return "Hello World! (up %s)\n" % elapsed()
40
41if __name__ == "__main__":
42    app.run(host="0.0.0.0", port=8080)

Created on 7/30/2018