~ubuntu-branches/ubuntu/trusty/plainbox-provider-checkbox/trusty

« back to all changes in this revision

Viewing changes to provider_bin/network_check

  • Committer: Package Import Robot
  • Author(s): Sylvain Pineau
  • Date: 2014-01-22 00:58:42 UTC
  • Revision ID: package-import@ubuntu.com-20140122005842-20pcic4y0ys439ry
Tags: upstream-0.3
ImportĀ upstreamĀ versionĀ 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
"""
 
3
Check that it's possible to establish a http connection against
 
4
ubuntu.com
 
5
"""
 
6
from subprocess import call
 
7
from argparse import ArgumentParser
 
8
import http.client
 
9
import urllib.request, urllib.error, urllib.parse
 
10
import sys
 
11
 
 
12
 
 
13
def check_url(url):
 
14
    """
 
15
    Open URL and return True if no exceptions were raised
 
16
    """
 
17
    try:
 
18
        urllib.request.urlopen(url)
 
19
    except (urllib.error.URLError, http.client.InvalidURL):
 
20
        return False
 
21
 
 
22
    return True
 
23
 
 
24
 
 
25
def main():
 
26
    """
 
27
    Check HTTP and connection
 
28
    """
 
29
    parser = ArgumentParser()
 
30
    parser.add_argument('-u', '--url',
 
31
                        action='store',
 
32
                        default='http://cdimage.ubuntu.com',
 
33
                        help='The target URL to try. Default is %(default)s')
 
34
    parser.add_argument('-a', '--auto',
 
35
                        action='store_true',
 
36
                        default=False,
 
37
                        help='Runs in Automated mode, with no visible output')
 
38
 
 
39
    args = parser.parse_args()
 
40
 
 
41
    url = {"http": args.url}
 
42
 
 
43
    results = {}
 
44
    for protocol, value in url.items():
 
45
        results[protocol] = check_url(value)
 
46
 
 
47
    bool2str = {True: 'Success', False: 'Failed'}
 
48
    message = ("HTTP connection: %(http)s\n"
 
49
               % dict([(protocol, bool2str[value])
 
50
                       for protocol, value in results.items()]))
 
51
 
 
52
    command = ["zenity", "--title=Network",
 
53
               "--text=%s" % message]
 
54
 
 
55
    if all(results.values()):
 
56
        command.append("--info")
 
57
    else:
 
58
        command.append("--error")
 
59
 
 
60
    if not args.auto:
 
61
        try:
 
62
            call(command)
 
63
        except OSError:
 
64
            print("Zenity missing; unable to report test result:\n %s" % message)
 
65
 
 
66
    if any(results.values()):
 
67
        return 0
 
68
    else:
 
69
        return 1
 
70
 
 
71
if __name__ == "__main__":
 
72
    sys.exit(main())