def test_validates_incorrect_plain_address(self): self.assertFalse(is_valid_ipv6_address('foo')) self.assertFalse(is_valid_ipv6_address('127.0.0.1')) self.assertFalse(is_valid_ipv6_address('12345::')) self.assertFalse(is_valid_ipv6_address('1::2:3::4')) self.assertFalse(is_valid_ipv6_address('1::zzz')) self.assertFalse(is_valid_ipv6_address('1::2:3:4:5:6:7:8')) self.assertFalse(is_valid_ipv6_address('1:2')) self.assertFalse(is_valid_ipv6_address('1:::2')) self.assertFalse(is_valid_ipv6_address('fe80::223: 6cff:fe8a:2e8a')) self.assertFalse(is_valid_ipv6_address('2a02::223:6cff :fe8a:2e8a'))
def test_validates_incorrect_plain_address(self): self.assertFalse(is_valid_ipv6_address("foo")) self.assertFalse(is_valid_ipv6_address("127.0.0.1")) self.assertFalse(is_valid_ipv6_address("12345::")) self.assertFalse(is_valid_ipv6_address("1::2:3::4")) self.assertFalse(is_valid_ipv6_address("1::zzz")) self.assertFalse(is_valid_ipv6_address("1::2:3:4:5:6:7:8")) self.assertFalse(is_valid_ipv6_address("1:2")) self.assertFalse(is_valid_ipv6_address("1:::2")) self.assertFalse(is_valid_ipv6_address("fe80::223: 6cff:fe8a:2e8a")) self.assertFalse(is_valid_ipv6_address("2a02::223:6cff :fe8a:2e8a"))
def country(self, query): """ Returns a dictonary with with the country code and name when given an IP address or a Fully Qualified Domain Name (FQDN). For example, both '24.124.1.80' and 'djangoproject.com' are valid parameters. """ name = None if is_valid_ipv4_address(query): code = self._country.country_code_by_addr(query) if code is not None: name = self._country.country_name_by_addr(query) elif is_valid_ipv6_address(query): code = self._country_v6.country_code_by_addr_v6(query) if code is not None: name = self._country_v6.country_name_by_addr_v6(query) else: code = self._country.country_code_by_name(query) if code is not None: name = self._country.country_name_by_name(query) if isinstance(code, six.binary_type): code = code.decode('latin_1', 'replace') if isinstance(name, six.binary_type): name = name.decode('latin_1', 'replace') return { 'country_code': code, 'country_name': name, }
def provider(self, query): """ Returns a tuple with the number and the name of the Internet service provider for the given IP Address or FQDN. """ information = { 'provider_number': None, 'provider_name': None, } if is_valid_ipv4_address(query): provider = self._asnum.org_by_addr(query) elif is_valid_ipv6_address(query): #provider = self._asnum_v6.org_by_addr_v6(query) provider = None else: provider = self._asnum.org_by_name(query) if provider is not None: provider = provider.decode('latin_1', 'replace') provider = provider.split(' ', 1) number = provider[0] name = provider[1] if len(provider) > 1 else '' information['provider_number'] = number information['provider_name'] = name return information
def host_represents_ip_address(host): """ Returns True if the host represents an IP address, possibly with a port number >>> host_represents_ip_address('example.com') False >>> host_represents_ip_address('10.0.0.1') True >>> host_represents_ip_address('10.0.0.1:80') True >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370:7348]') True >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370::::7348]') False >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370:7348]:') False >>> host_represents_ip_address('[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443') True """ host_split = host.split(':') if len(host_split) <= 2: # it might be an ipv4 address with optional port number if ipv4_re.match(host_split[0]): return True else: # it might be an ipv6 address match = ipv6_possible_re.match(host) if match and is_valid_ipv6_address(match.group(1)): return True return False
def _check_query(self, query, country=False, city=False, city_or_country=False): "Helper routine for checking the query and database availability." # Making sure a string was passed in for the query. if not isinstance(query, six.string_types): raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__) # Extra checks for the existence of country and city databases. if city_or_country and not (self._country or self._city): raise GeoIP2Exception('Invalid GeoIP country and city data files.') elif country and not self._country: raise GeoIP2Exception('Invalid GeoIP country data file: %s' % self._country_file) elif city and not self._city: raise GeoIP2Exception('Invalid GeoIP city data file: %s' % self._city_file) # Return the query string back to the caller. GeoIP2 only takes IP addresses. if not (ipv4_re.match(query) or is_valid_ipv6_address(query)): query = socket.gethostbyname(query) return query
def city(self, query): """ Returns a dictionary of city information for the given IP address or Fully Qualified Domain Name (FQDN). Some information in the dictionary may be undefined (None). """ information = { 'continent_code': None, 'country_code': None, 'country_code3': None, 'country_name': None, 'region': None, 'region_name': None, 'city': None, 'time_zone': None, 'area_code': 0, 'dma_code': 0, 'postal_code': None, 'latitude': 0.0, 'longitude': 0.0, } if is_valid_ipv4_address(query): record = self._city.record_by_addr(query) elif is_valid_ipv6_address(query): record = self._city_v6.record_by_addr_v6(query) else: record = self._city.record_by_name(query) if record is not None: for key, value in six.iteritems(record): if isinstance(value, six.binary_type): value = value.decode('latin_1', 'replace') information[key] = value return information
def build_url(host, scheme=None, port=None): """ Build a valid URL. IPv6 addresses specified in host will be enclosed in brackets automatically. >>> build_url('example.com', 'https', 443) 'https://example.com:443' >>> build_url(host='example.com', port=443) '//example.com:443' >>> build_url('fce:9af7:a667:7286:4917:b8d3:34df:8373', port=80, scheme='http') 'http://[fce:9af7:a667:7286:4917:b8d3:34df:8373]:80' :param scheme: The scheme, e.g. http, https or ftp. :type scheme: str :param host: Consisting of either a registered name (including but not limited to a hostname) or an IP address. :type host: str :type port: int :rtype: str """ netloc = host if not is_valid_ipv6_address(host) else '[{}]'.format(host) if port: netloc += ':{}'.format(port) pr = ParseResult(scheme=scheme, netloc=netloc, path='', params='', query='', fragment='') return pr.geturl()
def is_ip_allowed(self, ip): if not self.use_ip_auth: return True try: validators.validate_ipv46_address(ip) except ValidationError: return False if ipv6.is_valid_ipv6_address(ip): ip = ipv6.clean_ipv6_address(ip) return self._allowed_cleaned_ip(ip)
def test_validates_correct_plain_address(self): self.assertTrue(is_valid_ipv6_address("fe80::223:6cff:fe8a:2e8a")) self.assertTrue(is_valid_ipv6_address("2a02::223:6cff:fe8a:2e8a")) self.assertTrue(is_valid_ipv6_address("1::2:3:4:5:6:7")) self.assertTrue(is_valid_ipv6_address("::")) self.assertTrue(is_valid_ipv6_address("::a")) self.assertTrue(is_valid_ipv6_address("2::"))
def test_validates_correct_plain_address(self): self.assertTrue(is_valid_ipv6_address('fe80::223:6cff:fe8a:2e8a')) self.assertTrue(is_valid_ipv6_address('2a02::223:6cff:fe8a:2e8a')) self.assertTrue(is_valid_ipv6_address('1::2:3:4:5:6:7')) self.assertTrue(is_valid_ipv6_address('::')) self.assertTrue(is_valid_ipv6_address('::a')) self.assertTrue(is_valid_ipv6_address('2::'))
def test_validates_incorrect_with_v4mapping(self): self.assertFalse(is_valid_ipv6_address('::ffff:999.42.16.14')) self.assertFalse(is_valid_ipv6_address('::ffff:zzzz:0a0a')) # The ::1.2.3.4 format used to be valid but was deprecated # in rfc4291 section 2.5.5.1 self.assertTrue(is_valid_ipv6_address('::254.42.16.14')) self.assertTrue(is_valid_ipv6_address('::0a0a:0a0a')) self.assertFalse(is_valid_ipv6_address('::999.42.16.14')) self.assertFalse(is_valid_ipv6_address('::zzzz:0a0a'))
def _check_query(self, query, country=False, city=False, city_or_country=False): "Helper routine for checking the query and database availability." # Making sure a string was passed in for the query. if not isinstance(query, six.string_types): raise TypeError("GeoIP query must be a string, not type %s" % type(query).__name__) # Extra checks for the existence of country and city databases. if city_or_country and not (self._country or self._city): raise GeoIP2Exception("Invalid GeoIP country and city data files.") elif country and not self._country: raise GeoIP2Exception("Invalid GeoIP country data file: %s" % self._country_file) elif city and not self._city: raise GeoIP2Exception("Invalid GeoIP city data file: %s" % self._city_file) # Return the query string back to the caller. GeoIP2 only takes IP addresses. if not (ipv4_re.match(query) or is_valid_ipv6_address(query)): query = socket.gethostbyname(query) return query
def validate_ipv6_address(value): if not is_valid_ipv6_address(value): raise ValidationError(_('Enter a valid IPv6 address.'), code='invalid')
def validate_ipv6_address(value): if not is_valid_ipv6_address(value): raise ValidationError(_("Enter a valid IPv6 address."), code="invalid")
def save(self, *args, **kwargs): validators.validate_ipv46_address(self.ip) if ipv6.is_valid_ipv6_address(self.ip): self.ip = ipv6.clean_ipv6_address(self.ip) super(IPModel, self).save(*args, **kwargs)
def test_validates_correct_with_v4mapping(self): self.assertTrue(is_valid_ipv6_address('::ffff:254.42.16.14')) self.assertTrue(is_valid_ipv6_address('::ffff:0a0a:0a0a'))
def test_is_valid_ipv6_address(inp): is_valid_ipv6_address(inp)
# Making sure a string was passed in for the query. if not isinstance(query, str): >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__) # Extra checks for the existence of country and city databases. if city_or_country and not (self._country or self._city): raise GeoIP2Exception('Invalid GeoIP country and city data files.') elif country and not self._country: raise GeoIP2Exception('Invalid GeoIP country data file: %s' % self._country_file) elif city and not self._city: raise GeoIP2Exception('Invalid GeoIP city data file: %s' % self._city_file) # Return the query string back to the caller. GeoIP2 only takes IP addresses. <<<<<<< HEAD if not (ipv4_re.match(query) or is_valid_ipv6_address(query)): ======= try: validate_ipv46_address(query) except ValidationError: >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 query = socket.gethostbyname(query) return query def city(self, query): """ Return a dictionary of city information for the given IP address or Fully Qualified Domain Name (FQDN). Some information in the dictionary may be undefined (None). """
def is_valid_ip6( address ): return (is_valid_ipv6_address( address ))
def validate_ipv6_address(value): if not is_valid_ipv6_address(value): raise ValidationError('Enter a valid IPv6 address.', status_code=400)
import os
from __future__ import unicode_literals