Coverage for py_tools_ds/web/network.py: 0%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

18 statements  

1# -*- coding: utf-8 -*- 

2 

3# py_tools_ds - A collection of geospatial data analysis tools that simplify standard 

4# operations when handling geospatial raster and vector data as well as projections. 

5# 

6# Copyright (C) 2016-2021 

7# - Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de) 

8# - Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences Potsdam, 

9# Germany (https://www.gfz-potsdam.de/) 

10# 

11# This software was developed within the context of the GeoMultiSens project funded 

12# by the German Federal Ministry of Education and Research 

13# (project grant code: 01 IS 14 010 A-C). 

14# 

15# Licensed under the Apache License, Version 2.0 (the "License"); 

16# you may not use this file except in compliance with the License. 

17# You may obtain a copy of the License at 

18# 

19# http://www.apache.org/licenses/LICENSE-2.0 

20# 

21# Unless required by applicable law or agreed to in writing, software 

22# distributed under the License is distributed on an "AS IS" BASIS, 

23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

24# See the License for the specific language governing permissions and 

25# limitations under the License. 

26 

27import json 

28import socket 

29from urllib.request import urlopen 

30 

31 

32def get_geoinfo(): 

33 """Return a dictionary containing country, city, longitude, latitude and IP of the executing host.""" 

34 url = 'http://ipinfo.io/json' 

35 info = json.loads(urlopen(url).read()) 

36 ip = info['ip'] 

37 

38 urlFoLaction = "http://www.freegeoip.net/json/{0}".format(ip) 

39 locationInfo = json.loads(urlopen(urlFoLaction).read()) 

40 

41 return dict( 

42 Country=locationInfo['country_name'], 

43 City=locationInfo['city'], 

44 Latitude=str(locationInfo['latitude']), 

45 Longitude=str(locationInfo['longitude']), 

46 IP=str(locationInfo['ip']) 

47 ) 

48 

49 

50def is_connected(REMOTE_SERVER="www.google.com"): 

51 """Check if an internet connection is present.""" 

52 try: 

53 # see if we can resolve the host name -- tells us if there is 

54 # a DNS listening 

55 host = socket.gethostbyname(REMOTE_SERVER) 

56 # connect to the host -- tells us if the host is actually 

57 # reachable 

58 socket.create_connection((host, 80), 2) 

59 return True 

60 except Exception: 

61 pass 

62 return False