source: test_internet_connectivity.py

Last change on this file was d211dc3, checked in by Martin Kolman <martin.kolman@…>, 4 years ago

Applied coding style

  • Property mode set to 100755
File size: 3.4 KB
Line 
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3import urllib
4import urllib2
5
6# modRana has the same sockettimeout set
7import socket
8
9timeout = 30 # this sets timeout for all sockets
10socket.setdefaulttimeout(timeout)
11
12path = '/tmp/'
13filename1 = 'modRana_test_i_connectivity_google.gif'
14filename2 = 'modRana_test_i_connectivity_osm.png'
15google = 'http://www.google.com/intl/en_ALL/images/logo.gif'
16osm = 'http://b.tile.openstreetmap.org/17/71964/44884.png'
17
18print("###################################")
19print("\n###testing internet connectivity###")
20print("\n###################################")
21
22print("\n1. trying to download Google logo to /tmp/ using urllib")
23print("(%s)" % google)
24try:
25    urllib.urlretrieve(google, path + "urllib_" + filename1)
26    print("OK: Google logo + urllib")
27except Exception:
28    import sys
29
30    e = sys.exc_info()[1]
31    print("error while downloading Google logo using urllib: %s" % e)
32
33print("\n2. trying to download an OSM tile to /tmp/ using urllib")
34print("(%s)" % osm)
35try:
36    urllib.urlretrieve(osm, path + "urllib_" + filename2)
37    print("OK: OSM tile + urllib")
38except Exception:
39    import sys
40
41    e = sys.exc_info()[1]
42    print("error while downloading an OSM tile using urllib: %s" % e)
43
44print("\n3. trying to download Google logo to /tmp/ using urllib2")
45print("(%s)" % google)
46try:
47    req1 = urllib2.Request(google)
48    reply1 = urllib2.urlopen(req1)
49    string1 = path + "urllib2_" + filename1
50    file1 = open(string1, 'w')
51    file1.write(reply1.read())
52    file1.close()
53    print("OK: Google logo + urllib2")
54except Exception:
55    import sys
56
57    e = sys.exc_info()[1]
58    print("error while downloading Google logo using urllib2: %s" % e)
59
60print("\n4. trying to download an OSM tile to /tmp/ using urllib2")
61print("(%s)" % osm)
62try:
63    req2 = urllib2.Request(osm)
64    reply2 = urllib2.urlopen(req2)
65    string2 = path + "urllib2_" + filename2
66    file2 = open(string2, 'w')
67    file2.write(reply2.read())
68    file2.close()
69    print("OK: OSM tile + urllib2")
70except Exception:
71    import sys
72
73    e = sys.exc_info()[1]
74    print("error while downloading an OSM tile using urllib2: %s" % e)
75
76print("\n 5. trying to download an OSM tile using proxy:")
77
78# thanks to Slocan from http://talk.maemo.org/showthread.php?t=50570 for this testing code
79# Slocan reports, that he is setting proxy in his N900 application FeedingIt like this without problems
80def getProxy():
81    import gconf
82
83    if gconf.client_get_default().get_bool('/system/http_proxy/use_http_proxy'):
84        port = gconf.client_get_default().get_int('/system/http_proxy/port')
85        http = gconf.client_get_default().get_string('/system/http_proxy/host')
86        proxy = proxy = urllib2.ProxyHandler({"http": "http://%s:%s/" % (http, port)})
87        return True, proxy
88    return False, None
89
90# Enable proxy support
91(proxy_support, proxy) = getProxy()
92if proxy_support:
93    opener = urllib2.build_opener(proxy)
94    urllib2.install_opener(opener)
95
96### Use urllib2.urlopen("http://...")
97try:
98    f = urllib2.urlopen(osm)
99    data = f.read()
100    f.close()
101    string3 = path + "proxy_" + filename2
102    file3 = open(string3, 'w')
103    file3.write(data)
104    file3.close()
105    print("OK: OSM tile + proxy")
106except Exception:
107    import sys
108
109    e = sys.exc_info()[1]
110    print("error while downloading an OSM tile using proxy: %s" % e)
111
112print("\n######################")
113print("\n###testing complete###")
114print("\n######################")
115
Note: See TracBrowser for help on using the repository browser.