Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_geo_country(self, ip):
country = None
err = None
if ip is not None and self._geoip:
for candidate in str(ip).split(","):
candidate = candidate.strip()
if candidate == "":
continue
try:
country = self._geoip.country(candidate).country.iso_code
except AddressNotFoundError, e:
pass
except Exception, e:
err = e
# Return the first known country.
if country is not None:
return country
if err is not None:
raise err
return country
def get_location_from_ip(ip_address):
city = {}
if not ip_address:
return city
try:
geo = GeoIP2()
try:
city = geo.city(ip_address)
except AddressNotFoundError:
pass
except Exception as e:
logger.warning(
f"Encountered ({e}) while attempting to retrieve a user\'s geolocation") # Ignore LineLengthBear
return city
trace[s.ttl] = r.src
rt[trace_id] = trace
# Get the addresses locations
trt = {}
for trace_id in rt:
trace = rt[trace_id]
loctrace = []
for i in range(max(trace)):
ip = trace.get(i, None)
if ip is None:
continue
# Fetch database
try:
sresult = db.city(ip)
except geoip2.errors.AddressNotFoundError:
continue
loctrace.append((sresult.location.longitude, sresult.location.latitude)) # noqa: E501
if loctrace:
trt[trace_id] = loctrace
# Load the map renderer
plt.figure(num='Scapy')
ax = plt.axes(projection=ccrs.PlateCarree())
# Draw countries
ax.coastlines()
ax.stock_img()
# Set normal size
ax.set_global()
# Add title
plt.title("Scapy traceroute results")
def _get(self, database_type, ip_address):
if database_type not in self.metadata().database_type:
caller = inspect.stack()[2][3]
raise TypeError("The %s method cannot be used with the "
"%s database" %
(caller, self.metadata().database_type))
record = self._db_reader.get(ip_address)
if record is None:
raise geoip2.errors.AddressNotFoundError(
"The address %s is not in the database." % ip_address)
return record
subdivisions = "None"
ip_cache[ip_string]["subdivisions"] = subdivisions
# Find DataCenter Hosting Information from open source data
try:
dch_company = find_dch(ip_string, dch_dict)
if dch_company == "":
dch_company = " "
except AttributeError:
dch_company = " "
ip_cache[ip_string]["dch_company"] = dch_company
# MaxMind asn DB lookup
try:
asn_db_match = asn_db_reader.asn(ip_string)
except geoip2.errors.AddressNotFoundError:
sys.stderr.write("\n {} not found in ASN database.\n".format(ip_string))
asn_db_match = None
# Find ASN number from MaxMind ASN DB
try:
asn_number = asn_db_match.autonomous_system_number
except AttributeError:
asn_number = " "
ip_cache[ip_string]["asn_number"] = asn_number
# Find ASN organization name from MaxMind ASN DB
try:
asn_name = asn_db_match.autonomous_system_organization
if asn_name != None:
asn_name = unicodedata.normalize('NFKD',asn_name).encode('ascii','ignore')
except AttributeError:
def geoip(reader, ip):
try:
record = reader.city(ip)
except (AddressNotFoundError, ValueError):
return {}
if record is None:
return {}
result = {}
geoip_cc = record.country.iso_code
if geoip_cc:
result["geoip cc"] = [geoip_cc]
latitude = record.location.latitude
longitude = record.location.longitude
if latitude or longitude:
result["latitude"] = [unicode(latitude)]
result["longitude"] = [unicode(longitude)]
def geocode_ip(address):
aso = None
asn = None
country = None
city = None
if not address.endswith(".onion"):
try:
aso = ASN.asn(address).autonomous_system_organization
asn = ASN.asn(address).autonomous_system_number
except AddressNotFoundError:
pass
try:
country = COUNTRY.country(address).country.name
country = RENAMED_COUNTRIES.get(country, country)
city = CITY.city(address).city.name
except AddressNotFoundError:
pass
return country, city, aso, asn