diff -Nru my-weather-indicator-0.9.0/debian/changelog my-weather-indicator-0.9.0/debian/changelog --- my-weather-indicator-0.9.0/debian/changelog 2017-11-08 19:38:43.000000000 +0000 +++ my-weather-indicator-0.9.0/debian/changelog 2017-11-09 19:08:25.000000000 +0000 @@ -1,10 +1,10 @@ -my-weather-indicator (0.9.0-0extras17.10.2) artful; urgency=medium +my-weather-indicator (0.9.0-0extras17.10.4) artful; urgency=medium * Updated location * Fixed some bugs * check_connection updated with two options - -- Lorenzo Carbonell Wed, 08 Nov 2017 20:38:43 +0100 + -- Lorenzo Carbonell Thu, 09 Nov 2017 20:08:25 +0100 my-weather-indicator (0.8.1-0extras16.04.1) xenial; urgency=medium diff -Nru my-weather-indicator-0.9.0/src/check_connection.py my-weather-indicator-0.9.0/src/check_connection.py --- my-weather-indicator-0.9.0/src/check_connection.py 2017-11-08 19:37:27.000000000 +0000 +++ my-weather-indicator-0.9.0/src/check_connection.py 2017-11-09 19:07:40.000000000 +0000 @@ -1,9 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# A library for access to geocode for address -# -# Copyright (C) 2011-2016 Lorenzo Carbonell +# A library for access to check connectivity +# Copyright (C) 2011-2017 Lorenzo Carbonell # lorenzo.carbonell.cerezo@gmail.com # # This program is free software: you can redistribute it and/or modify @@ -19,98 +18,94 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . - +import http.client import requests -# import urllib.request -import urllib.parse import socket import time -# import http -# import http.client def check_connectivity(): - if check_connectivity3() or check_connectivity2('https://www.google.com'): - return True + sockets = ['www.google.com', + '216.58.192.142', + 'www.baidu.com'] + for asocket in sockets: + if check_connectivity_with_httpconnection(asocket): + return True + for asocket in sockets: + if check_connectivity_with_socket(asocket): + return True + urls = ['https://www.google.com', + 'http://www.google.com', + 'http://216.58.192.142', + 'http://www.baidu.com'] + for url in urls: + if check_connectivity_with_reference(url): + return True return False -def check_connectivity1(reference): +def check_connectivity_with_httpconnection(reference): try: - urllib.request.urlopen(reference, timeout=1) + conn = http.client.HTTPConnection(reference) + conn.close() + print('OK. Internet connection. HTTPConnection: {0}'.format(reference)) return True - except urllib.request.URLError: - return False - - -def check_connectivity2(reference, timeout=5): - try: - requests.get(reference, timeout=timeout, verify=False) - return True - except requests.ConnectionError: - print("No internet connection available.") + except Exception as ex: + print('NO internet connection. HTTPConnection: {0}'.format(reference)) + print('Error:', ex) + print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^') return False -def check_connectivity3(host="8.8.8.8", port=53): - """ - Host: 8.8.8.8 (google-public-dns-a.google.com) - OpenPort: 53/tcp - Service: domain (DNS/TCP) - """ +def check_connectivity_with_socket(reference, port=80): try: - socket.setdefaulttimeout(1) - socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port)) + conn = socket.create_connection((reference, port)) + conn.close() + print('OK. Internet connection. Socket: {0}'.format(reference)) return True except Exception as ex: - print('No internet connection available.') - print('^^^^^', ex, '^^^^^') + print('NO internet connection. Socket: {0}'.format(reference)) + print('Error:', ex) + print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^') return False -def check_connectivity4(): - conn = http.client.HTTPConnection("www.google.com") +def check_connectivity_with_reference(reference, timeout=2): try: - conn.request("HEAD", "/") + requests.get(reference, timeout=timeout, verify=False) + print('OK. Internet connection. Url: {0}'.format(reference)) return True except Exception as ex: - print('No internet connection available.') - print('^^^^^', ex, '^^^^^') - return False - - -def check_connectivity5(): - conn_url = 'https://www.google.com/' - try: - data = urllib.request.urlopen(conn_url, timeout=5) - except Exception as ex: - print('No internet connection available.') - print('^^^^^', ex, '^^^^^') - return False - try: - host = data.fp._sock.fp._sock.getpeername() - except AttributeError: # Python 3 - host = data.fp.raw._sock.getpeername() - - # Ensure conn_url is an IPv4 address otherwise future queries will fail - conn_url = 'http://' + ( - host[0] if len(host) == 2 else socket.gethostbyname( - urllib.parse.urlparse(data.geturl()).hostname)) - return True + print('NO internet connection. Url: {0}'.format(reference)) + print('Error:', ex) + print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^') + return False if __name__ == '__main__': - TESTURL = 'https://www.google.com' - atime = time.time() - print(1, check_connectivity(), time.time() - atime) - ''' - print(1, check_connectivity1(TESTURL), time.time() - atime) - atime = time.time() - print(2, check_connectivity2(TESTURL), time.time() - atime) - atime = time.time() - print(3, check_connectivity3(), time.time() - atime) - atime = time.time() - print(4, check_connectivity4(), time.time() - atime) atime = time.time() - print(5, check_connectivity5(), time.time() - atime) + print(check_connectivity(), time.time() - atime) ''' + urls = ['https://www.google.com', + 'http://www.google.com', + 'http://216.58.192.142', + 'http://www.baidu.com'] + sockets = ['www.google.com', + '216.58.192.142', + 'www.baidu.com'] + print('======== SOCKET ========') + for index, asocket in enumerate(sockets): + atime = time.time() + print(index, check_connectivity_with_httpconnection(asocket), asocket, + time.time() - atime) + print('======== SOCKET ========') + for index, asocket in enumerate(sockets): + atime = time.time() + print(index, check_connectivity_with_socket(asocket), asocket, + time.time() - atime) + print('======== URLS ========') + for index, url in enumerate(urls): + atime = time.time() + print(index, check_connectivity_with_reference(url), url, + time.time() - atime) + ''' \ No newline at end of file diff -Nru my-weather-indicator-0.9.0/src/geolocation.py my-weather-indicator-0.9.0/src/geolocation.py --- my-weather-indicator-0.9.0/src/geolocation.py 2017-07-04 14:55:26.000000000 +0000 +++ my-weather-indicator-0.9.0/src/geolocation.py 2017-11-09 18:50:00.000000000 +0000 @@ -50,7 +50,7 @@ if __name__ == '__main__': ip = get_external_ip() if ip is not None: - ll = get_latitude_longitude(ip) + ll = get_latitude_longitude_city(ip) if ll is not None: print(ll) exit(0)