Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

basic auth: Implement HTTP Basic Auth and configuration through envir…

…onment vars
  • Loading branch information...
commit 91a2174cf98fc57a0315d804abb3684913a49a6b 1 parent 410d1c8
@hltbra hltbra authored
Showing with 56 additions and 7 deletions.
  1. +20 −0 README.md
  2. +26 −5 pinger.py
  3. +1 −0  sites.txt
  4. +9 −2 templates/pinger.html
View
20 README.md
@@ -3,3 +3,23 @@ URL Pinger
It pings a URL and follows redirects, and report FAIL or SUCCESS. That's it.
+
+How to Use
+----------
+
+ $ git clone https://github.com/globocom/url-pinger.git
+ $ cd url-pinger
+ $ pip install -r requirements.txt
+ $ echo http://www.globo.com > public_sites.txt
+ $ export SITES_TXT=public_sites.txt
+ $ python pinger.py &
+ $ open http://localhost:5000
+
+
+Sites.txt Format
+----------------
+
+ protocol://url1
+ protocol://url2 user:password
+
+See https://raw.github.com/globocom/url-pinger/master/sites.txt
View
31 pinger.py
@@ -1,26 +1,47 @@
-from flask import Flask, render_template
+import os
import datetime
import socket
import requests
+from flask import Flask, render_template
+
+SITES_TXT = os.getenv("SITES_TXT", "sites.txt")
+REQUEST_TIMEOUT = float(os.getenv("REQUEST_TIMEOUT", "1"))
+REFRESH_TIMEOUT = float(os.getenv("REFRESH_TIMEOUT", "60"))
app = Flask(__name__)
+
@app.route('/')
def home():
- sites = open("sites.txt").read().split()
+ sites = open(SITES_TXT).readlines()
result = []
- for site in sites:
+ for line in sites:
+ site, auth = extract_site_and_auth(line.strip())
try:
- response = requests.get(site, timeout=1, allow_redirects=True)
+ response = requests.get(site, timeout=REQUEST_TIMEOUT, allow_redirects=True, auth=auth)
assert response.text != ''
assert response.status_code == 200
except (requests.exceptions.RequestException, socket.timeout, AssertionError) as err:
+ print("Failed to read {0} (timeout={1})".format(site, REQUEST_TIMEOUT))
result.append((site, "fail"))
else:
result.append((site, "ok"))
now = datetime.datetime.now()
- return render_template('pinger.html', result=result, last_update=now)
+ template_vars = dict(
+ result=result,
+ last_update=now,
+ refresh_timeout=REFRESH_TIMEOUT)
+ return render_template('pinger.html', **template_vars)
+
+
+def extract_site_and_auth(line):
+ if ' ' in line:
+ site, auth = line.rsplit(' ', 1)
+ user, password = auth.split(':')
+ return (site, (user, password))
+ else:
+ return [line, ()]
if __name__ == '__main__':
View
1  sites.txt
@@ -1 +1,2 @@
http://globo.com
+http://httpbin.org/basic-auth/user/passwd user:passwd
View
11 templates/pinger.html
@@ -13,17 +13,24 @@
padding: 0.75em 1.2em;
position: relative;
}
+
.ok {
background-color: green;
}
+
.fail {
background-color: red;
}
+
.status {
font-size: 20px;
}
+
+ .last-update {
+ margin-top: 20px;
+ }
</style>
- <meta http-equiv="Refresh" content="60"/>
+ <meta http-equiv="Refresh" content="{{ refresh_timeout }}" />
</head>
<body>
<div class="content">
@@ -39,7 +46,7 @@
</tr>
{% endfor %}
</table>
- Last update: {{ last_update }}
+ <div class="last-update">Last update: {{ last_update }}</div>
</div>
</body>
</html>
Please sign in to comment.
Something went wrong with that request. Please try again.