Last active
December 17, 2015 00:49
-
-
Save sysr-q/5524365 to your computer and use it in GitHub Desktop.
This just queries the bloocoin server periodically and returns JSON info.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify | |
import threading | |
import socket | |
import time | |
app = Flask(__name__) | |
_timer = 60 * 5 | |
_timeout = 3 | |
info = { | |
"alive": False, | |
"started": time.time(), | |
"ended": time.time(), | |
"time": 0, | |
"result": "" | |
} | |
def worker(): | |
while True: | |
started = time.time() | |
try: | |
s = socket.socket() | |
s.settimeout(_timeout) | |
s.connect(("bloocoin.zapto.org", 3122)) | |
s.close() | |
info['alive'] = True | |
info['result'] = "" | |
except IOError as e: | |
info['alive'] = False | |
info['result'] = str(e) | |
ended = time.time() | |
info['started'] = started | |
info['ended'] = ended | |
info['time'] = ended - started | |
time.sleep(_timer) | |
@app.route("/") | |
def index(): | |
return "".join([ | |
"<!DOCTYPE html>\n", | |
"<html><head>", | |
"<title>BlooCoin: {word}! {smile}</title>", | |
"</head><body>", | |
"The server (<b>bloocoin.zapto.org:3122</b>)", | |
" is currently: <b>{word}</b>! {smile}", | |
"<br>", | |
"You can get JSON related info at <b>/status</b>", | |
"</body></html>" | |
]).format( | |
word="alive" if info['alive'] else "dead", | |
smile=":)" if info['alive'] else ":(" | |
) | |
@app.route("/status") | |
def status(): | |
return jsonify(info) | |
t = threading.Thread(target=worker) | |
t.daemon = True | |
t.start() | |
app.run(port=3123) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment