|
2 | 2 | For functions mucking with internet access |
3 | 3 | """ |
4 | 4 | import ifcfg |
| 5 | +import logging |
5 | 6 | import os |
6 | 7 | import platform |
7 | 8 | import re |
8 | 9 | import requests |
9 | | -import socket |
10 | 10 |
|
11 | | -from urlparse import parse_qs, urlsplit, urlunsplit |
| 11 | + |
| 12 | +from django.conf import settings |
| 13 | +from django.core.urlresolvers import reverse |
| 14 | +from urlparse import parse_qs, urlsplit, urlunsplit, urljoin |
12 | 15 | from urllib import urlencode |
13 | 16 |
|
14 | 17 |
|
15 | | -def am_i_online(url, expected_val=None, search_string=None, timeout=5, allow_redirects=True): |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +def am_i_online(): |
16 | 22 | """Test whether we are online or not. |
17 | 23 | returns True or False. |
18 | | - Eats all exceptions! |
| 24 | + Eats all exceptions! <- great :( /benjaoming |
19 | 25 | """ |
20 | | - assert not (search_string and expected_val is not None), "Search string and expected value cannot both be set" |
21 | | - |
22 | 26 | from kalite.version import user_agent |
23 | 27 |
|
| 28 | + url = urljoin(settings.CENTRAL_SERVER_URL, reverse("get_server_info")) |
| 29 | + |
24 | 30 | try: |
25 | | - if not search_string and expected_val is None: |
26 | | - response = requests.head(url, headers={"user-agent": user_agent()}) |
27 | | - else: |
28 | | - response = requests.get(url, timeout=timeout, allow_redirects=allow_redirects, headers={"user-agent": user_agent()}) |
| 31 | + response = requests.get(url, timeout=5, allow_redirects=False, headers={"user-agent": user_agent()}) |
29 | 32 |
|
30 | 33 | # Validate that response came from the requested url |
31 | 34 | if response.status_code != 200: |
| 35 | + |
| 36 | + logger.warning("Unexpected response detecting online status: {}".format(response)) |
32 | 37 | return False |
33 | | - elif not allow_redirects and response.url != url: |
34 | | - return False |
35 | | - |
36 | | - # Check the output, if expected values are specified |
37 | | - if expected_val is not None: |
38 | | - return expected_val == response.text |
39 | | - elif search_string: |
40 | | - return search_string in response.text |
41 | 38 |
|
42 | 39 | return True |
43 | 40 |
|
44 | 41 | except Exception as e: |
| 42 | + logger.warning("Unhandled exception when detecting if online: {}".format(e)) |
45 | 43 | return False |
46 | 44 |
|
47 | 45 |
|
|
0 commit comments