def get(ip_address, api_key=None, db_path=None, username=None, password=None): # process request try: request = requests.get('https://ipinfo.io/' + quote(ip_address) + '/geo/', timeout=62) except: raise ServiceError() # check for HTTP errors if request.status_code != 200: if request.status_code == 404: raise IpAddressNotFoundError(ip_address) elif request.status_code == 429: raise LimitExceededError() elif request.status_code == 500: raise InvalidRequestError() else: raise ServiceError() # parse content try: content = request.content.decode('utf-8') content = json.loads(content) except: raise InvalidResponseError() # prepare return value ip_location = IpLocation(ip_address) # format data if content.get('country'): ip_location.country = content['country'] else: ip_location.country = None if content.get('region'): ip_location.region = content['region'] else: ip_location.region = None if content.get('city'): ip_location.city = content['city'] else: ip_location.city = None if content.get('loc'): location = content['loc'].split(',') ip_location.latitude = float(location[0]) ip_location.longitude = float(location[1]) else: ip_location.latitude = None ip_location.longitude = None return ip_location
def get(ip_address, api_key, db_path=None, username=None, password=None): # process request try: request = requests.get('http://api.db-ip.com/v2/' + quote(api_key) + '/' + quote(ip_address), timeout=62) except: raise ServiceError() # check for HTTP errors if request.status_code != 200: raise ServiceError() # parse content try: content = request.content.decode('utf-8') content = json.loads(content) except: raise InvalidResponseError() # check for errors if content.get('error'): if content['error'] == 'invalid address': raise IpAddressNotFoundError(ip_address) elif content['error'] == 'invalid API key': raise PermissionRequiredError() else: raise InvalidRequestError() # prepare return value ip_location = IpLocation(ip_address) # format data ip_location.country = content['countryCode'] ip_location.region = content['stateProv'] ip_location.city = content['city'] # get lat/lon from OSM osm = geocoder.osm(ip_location.city + ', ' + ip_location.region + ' ' + ip_location.country, timeout=62) if osm.ok: osm = osm.json ip_location.latitude = float(osm['lat']) ip_location.longitude = float(osm['lng']) else: osm = geocoder.osm(ip_location.city + ', ' + ip_location.country, timeout=62) if osm.ok: osm = osm.json ip_location.latitude = float(osm['lat']) ip_location.longitude = float(osm['lng']) else: ip_location.latitude = None ip_location.longitude = None return ip_location
def get(ip_address, api_key=None, db_path=None, username=None, password=None): # process request try: request = requests.get( 'http://api.hostip.info/get_json.php?position=true&ip=' + quote(ip_address), timeout=62) except: raise ServiceError() # check for HTTP errors if request.status_code != 200: if request.status_code == 404: raise IpAddressNotFoundError(ip_address) elif request.status_code == 500: raise InvalidRequestError() else: raise ServiceError() # parse content try: content = request.content.decode('utf-8') content = json.loads(content) except: raise InvalidResponseError() # prepare return value ip_location = IpLocation(ip_address) # format data if content['country_code'] == 'XX': ip_location.country = None else: ip_location.country = content['country_code'] ip_location.region = None if content['city'] == '(Unknown City?)' \ or content['city'] == '(Unknown city)' \ or content['city'] == '(Private Address)': ip_location.city = None else: ip_location.city = content['city'] if content.get('lat') and content.get('lng'): ip_location.latitude = float(content['lat']) ip_location.longitude = float(content['lng']) else: ip_location.latitude = None ip_location.longitude = None return ip_location
def get(ip_address, api_key=None, db_path=None, username=None, password=None): # process request try: request = requests.get('http://freegeoip.net/json/' + quote(ip_address), timeout=62) except: raise ServiceError() # check for HTTP errors if request.status_code != 200: if request.status_code == 404: raise IpAddressNotFoundError(ip_address) elif request.status_code == 500: raise InvalidRequestError() else: raise ServiceError() # parse content try: content = request.content.decode('utf-8') content = json.loads(content) except: raise InvalidResponseError() # prepare return value ip_location = IpLocation(ip_address) # format data if content['country_code'] == '': ip_location.country = None else: ip_location.country = content['country_code'] if content['region_name'] == '': ip_location.region = None else: ip_location.region = content['region_name'] if content['city'] == '': ip_location.city = None else: ip_location.city = content['city'] if content['latitude'] != '-' and content['longitude'] != '-': ip_location.latitude = float(content['latitude']) ip_location.longitude = float(content['longitude']) else: ip_location.latitude = None ip_location.longitude = None return ip_location
def get(ip_address, api_key=None, db_path=None, username=None, password=None): # process request try: request = geoip2.database.Reader(db_path) except: raise ServiceError() # content try: res = request.city(ip_address) except TypeError: raise InvalidRequestError() except geoip2.errors.AddressNotFoundError: raise IpAddressNotFoundError(ip_address) # prepare return value ip_location = IpLocation(ip_address) # format data if res.country: ip_location.country = res.country.iso_code else: ip_location.country = None if res.subdivisions: ip_location.region = res.subdivisions[0].names['en'] else: ip_location.region = None if res.city.names: ip_location.city = res.city.names['en'] else: ip_location.city = None if res.location: ip_location.latitude = float(res.location.latitude) ip_location.longitude = float(res.location.longitude) else: ip_location.latitude = None ip_location.longitude = None return ip_location
def get(ip_address, api_key=None, db_path=None, username=None, password=None): # process request try: request = requests.get('http://api.ipstack.com/' + quote(ip_address) + '?access_key=' + quote(api_key), timeout=62) except: raise ServiceError() # check for HTTP errors if request.status_code != 200: raise ServiceError() # parse content try: content = request.content.decode('utf-8') content = json.loads(content) except: raise InvalidResponseError() # check for errors if content.get('error'): if content['error']['code'] == 101 \ or content['error']['code'] == 102 \ or content['error']['code'] == 105: raise PermissionRequiredError() elif content['error']['code'] == 104: raise LimitExceededError() else: raise InvalidRequestError() # prepare return value ip_location = IpLocation(ip_address) # format data if content['country_code'] == '': ip_location.country = None else: ip_location.country = content['country_code'] if content['region_name'] == '': ip_location.region = None else: ip_location.region = content['region_name'] if content['city'] == '': ip_location.city = None else: ip_location.city = content['city'] if content['latitude'] != '-' and content['longitude'] != '-': ip_location.latitude = float(content['latitude']) ip_location.longitude = float(content['longitude']) else: ip_location.latitude = None ip_location.longitude = None return ip_location
def get(ip_address, api_key=None, db_path=None, username=None, password=None): # process request try: request = requests.get( 'https://https-api.eurekapi.com/iplocation/v1.8/locateip?' + 'ip=' + quote(ip_address) + '&key=' + quote(api_key) + '&format=JSON', timeout=62) except: raise ServiceError() # check for HTTP errors if request.status_code != 200: if request.status_code == 429: raise LimitExceededError() elif request.status_code == 500: raise InvalidRequestError() else: raise ServiceError() # parse content try: content = request.content.decode('utf-8') content = json.loads(content) except: raise InvalidResponseError() # prepare return value ip_location = IpLocation(ip_address) # check for errors if content['query_status']['query_status_code'] != 'OK': error_status = content['query_status']['query_status_code'] error_status_desc = content['query_status'][ 'query_status_description'] if error_status == 'MISSING_SERVICE_ACCESS_KEY' \ or error_status == 'INVALID_SERVICE_ACCESS_KEY' \ or error_status == 'FREE_TRIAL_LICENSE_EXPIRED' \ or error_status == 'SUBSCRIPTION_EXPIRED': raise PermissionRequiredError(error_status_desc) elif error_status == 'MISSING_IP_ADDRESS' \ or error_status == 'INVALID_IP_ADDRESS': raise IpAddressNotFoundError(ip_address) else: ip_location.country = None ip_location.region = None ip_location.city = None ip_location.latitude = None ip_location.longitude = None return ip_location # format data if content.get('geolocation_data'): if content['geolocation_data'].get('country_code_iso3166alpha2'): ip_location.country = content['geolocation_data'][ 'country_code_iso3166alpha2'] else: ip_location.country = None if content['geolocation_data'].get('region_name'): ip_location.region = content['geolocation_data']['region_name'] else: ip_location.region = None if content['geolocation_data'].get('city'): ip_location.city = content['geolocation_data']['city'] else: ip_location.city = None if content['geolocation_data'].get('latitude') \ and content['geolocation_data'].get('longitude'): ip_location.latitude = float( content['geolocation_data']['latitude']) ip_location.longitude = float( content['geolocation_data']['longitude']) else: ip_location.latitude = None ip_location.longitude = None else: ip_location.country = None ip_location.region = None ip_location.city = None ip_location.latitude = None ip_location.longitude = None return ip_location
def get(ip_address, api_key=None, db_path=None, username=None, password=None): # process request try: request = requests.get( 'https://context.skyhookwireless.com/accelerator/ip?' + 'ip=' + quote(ip_address) + '&user='******'&key=' + quote(password) + '&version=2.0', timeout=62) except: raise ServiceError() # check for HTTP errors if request.status_code != 200: if request.status_code == 400: raise InvalidRequestError() elif request.status_code == 401: raise PermissionRequiredError(ip_address) else: raise ServiceError() # content decode try: content = request.content.decode('utf-8') except: raise InvalidResponseError() # check for IP address not found error if content == '{"data":{"ip":"' + ip_address + '"}}': raise IpAddressNotFoundError(ip_address) # parse content try: content = json.loads(content) except: raise InvalidResponseError() # prepare return value ip_location = IpLocation(ip_address) # format data if content.get('data'): if content['data'].get('civic'): if content['data']['civic'].get('countryIso'): ip_location.country = content['data']['civic'][ 'countryIso'] else: ip_location.country = None if content['data']['civic'].get('state'): ip_location.region = content['data']['civic']['state'] else: ip_location.region = None if content['data']['civic'].get('city'): ip_location.city = content['data']['civic']['city'] else: ip_location.city = None else: ip_location.country = None ip_location.region = None ip_location.city = None if content['data'].get('location'): if content['data']['location'].get('latitude') \ and content['data']['location'].get('longitude'): ip_location.latitude = content['data']['location'][ 'latitude'] ip_location.longitude = content['data']['location'][ 'longitude'] else: ip_location.latitude = None ip_location.longitude = None else: ip_location.latitude = None ip_location.longitude = None else: ip_location.country = None ip_location.region = None ip_location.city = None ip_location.latitude = None ip_location.longitude = None return ip_location
def get(ip_address, api_key=None, db_path=None, username=None, password=None): # process request try: # optional auth for increasing amount of queries per day if username != None and password != None: auth = HTTPBasicAuth(username, password) else: auth = None request = requests.get('https://www.maxmind.com/geoip/v2.1/city/' + quote(ip_address) + ('?demo=1' if auth == None else ''), auth=auth, timeout=62) except: raise ServiceError() # parse content try: content = request.content.decode('utf-8') content = json.loads(content) except: raise InvalidResponseError() # check for HTTP errors if request.status_code != 200: if request.status_code == 400: raise InvalidRequestError(content['code']) elif request.status_code == 401: raise PermissionRequiredError(content['code']) elif request.status_code == 402: raise LimitExceededError(content['code']) elif request.status_code == 403: raise PermissionRequiredError(content['code']) elif request.status_code == 404: raise IpAddressNotFoundError(ip_address) elif request.status_code == 500: raise InvalidRequestError() else: raise ServiceError() # prepare return value ip_location = IpLocation(ip_address) # format data if content.get('country'): if content['country'].get('iso_code'): ip_location.country = content['country']['iso_code'] else: ip_location.country = None else: ip_location.country = None if content.get('subdivisions'): if content['subdivisions'][0].get('names'): if content['subdivisions'][0]['names'].get('en'): ip_location.region = content['subdivisions'][0]['names'][ 'en'] else: ip_location.region = None else: ip_location.region = None else: ip_location.region = None if content.get('city'): if content['city'].get('names'): if content['city']['names'].get('en'): ip_location.city = content['city']['names']['en'] else: ip_location.city = None else: ip_location.city = None else: ip_location.city = None if content.get('location'): ip_location.latitude = float(content['location']['latitude']) ip_location.longitude = float(content['location']['longitude']) else: ip_location.latitude = None ip_location.longitude = None return ip_location
def get(ip_address, api_key='test', db_path=None, username=None, password=None): # process request try: request = requests.get('https://api.ipdata.co/' + quote(ip_address) + '?api-key=' + quote(api_key), timeout=62) except: raise ServiceError() # check for HTTP errors if request.status_code != 200 and request.status_code != 400: if request.status_code == 401: raise PermissionRequiredError() elif request.status_code == 403: raise LimitExceededError() else: raise ServiceError() # parse content try: content = request.content.decode('utf-8') content = json.loads(content) except: raise InvalidResponseError() # check for errors if content.get('message'): if 'private IP address' in content['message']: raise IpAddressNotFoundError(ip_address) else: raise InvalidRequestError() # prepare return value ip_location = IpLocation(ip_address) # format data if content['country_code'] == '': ip_location.country = None else: ip_location.country = content['country_code'] if content['region'] == '': ip_location.region = None else: ip_location.region = content['region'] if content['city'] == '': ip_location.city = None else: ip_location.city = content['city'] if content['latitude'] != '-' and content['longitude'] != '-': ip_location.latitude = float(content['latitude']) ip_location.longitude = float(content['longitude']) else: ip_location.latitude = None ip_location.longitude = None return ip_location