-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatsE16-server.py
73 lines (59 loc) · 1.8 KB
/
statsE16-server.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import logging
import psutil
import subprocess
import os
import yaml
import ipaddress
from flask import Flask
from flask import request
from two1.wallet.two1_wallet import Wallet
from two1.bitserv.flask import Payment
from statsE16 import statsE16
app = Flask(__name__)
# setup wallet
wallet = Wallet()
payment = Payment(app, wallet)
# hide logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
@app.route('/')
@payment.required(5)
def measurement():
""" Queries the local device for stats details
Returns: HTTPResponse 200 with a json containing the stats info.
HTTP Response 400 if there is an error reading the stats.
"""
try:
data = statsE16()
response = json.dumps(data, indent=4, sort_keys=True)
return response
except ValueError as e:
return 'HTTP Status 400: {}'.format(e.args[0]), 400
if __name__ == '__main__':
import click
@click.command()
@click.option("-d", "--daemon", default=False, is_flag=True,
help="Run in daemon mode.")
def run(daemon):
if daemon:
pid_file = './statsE16.pid'
if os.path.isfile(pid_file):
pid = int(open(pid_file).read())
os.remove(pid_file)
try:
p = psutil.Process(pid)
p.terminate()
except:
pass
try:
p = subprocess.Popen(['python3', 'statsE16-server.py'])
open(pid_file, 'w').write(str(p.pid))
except subprocess.CalledProcessError:
raise ValueError("error starting statsE16-server.py daemon")
else:
print("Server running...")
app.run(host='::', port=7016)
run()