SystemDownload: test_internet_connectivity.py

File test_internet_connectivity.py, 3.1 KB (added by Martin Kolman, 9 years ago)

a Python script for testing internet connectivity

Line 
1#!/usr/bin/python
2import urllib
3import urllib2
4
5# modRana has the same sockettimeout set
6import socket
7timeout = 30 # this sets timeout for all sockets
8socket.setdefaulttimeout(timeout)
9
10path = '/tmp/'
11filename1 = 'modRana_test_i_connectivity_google.gif'
12filename2 = 'modRana_test_i_connectivity_osm.png'
13google = 'http://www.google.com/intl/en_ALL/images/logo.gif'
14osm = 'http://b.tile.openstreetmap.org/17/71964/44884.png'
15
16print "###################################"
17print "\n###testing internet connectivity###"
18print "\n###################################"
19
20
21print "\n1. trying to download Google logo to /tmp/ using urllib"
22print "(%s)" % google
23try:
24  urllib.urlretrieve(google, path+"urllib_"+filename1)
25  print "OK: Google logo + urllib"
26except Exception, e:
27  print "error while downloading Google logo using urllib: %s" % e
28
29
30print "\n2. trying to download an OSM tile to /tmp/ using urllib"
31print "(%s)" % osm
32try:
33  urllib.urlretrieve(osm, path+"urllib_"+filename2)
34  print "OK: OSM tile + urllib"
35except Exception, e:
36  print "error while downloading an OSM tile using urllib: %s" % e
37
38
39print "\n3. trying to download Google logo to /tmp/ using urllib2"
40print "(%s)" % google
41try:
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"
49except Exception, e:
50  print "error while downloading Google logo using urllib2: %s" % e
51
52print "\n4. trying to download an OSM tile to /tmp/ using urllib2"
53print "(%s)" % osm
54try:
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"
62except Exception, e:
63  print "error while downloading an OSM tile using urllib2: %s" % e
64
65
66print "\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
70def 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()
81if proxy_support:
82    opener = urllib2.build_opener(proxy)
83    urllib2.install_opener(opener)
84
85### Use urllib2.urlopen("http://...")
86try:
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"
95except Exception, e:
96  print "error while downloading an OSM tile using proxy: %s" % e
97
98
99
100
101
102print "\n######################"
103print "\n###testing complete###"
104print "\n######################"
105