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