当前位置: 首页>>代码示例>>Python>>正文


Python encodings.idna方法代码示例

本文整理汇总了Python中encodings.idna方法的典型用法代码示例。如果您正苦于以下问题:Python encodings.idna方法的具体用法?Python encodings.idna怎么用?Python encodings.idna使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在encodings的用法示例。


在下文中一共展示了encodings.idna方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_nameprep

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def test_nameprep(self):
        from encodings.idna import nameprep
        for pos, (orig, prepped) in enumerate(nameprep_tests):
            if orig is None:
                # Skipped
                continue
            # The Unicode strings are given in UTF-8
            orig = str(orig, "utf-8", "surrogatepass")
            if prepped is None:
                # Input contains prohibited characters
                self.assertRaises(UnicodeError, nameprep, orig)
            else:
                prepped = str(prepped, "utf-8", "surrogatepass")
                try:
                    self.assertEqual(nameprep(orig), prepped)
                except Exception as e:
                    raise support.TestFailed("Test 3.%d: %s" % (pos+1, str(e))) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:19,代码来源:test_codecs.py

示例2: test_builtin_decode

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def test_builtin_decode(self):
        self.assertEqual(str(b"python.org", "idna"), "python.org")
        self.assertEqual(str(b"python.org.", "idna"), "python.org.")
        self.assertEqual(str(b"xn--pythn-mua.org", "idna"), "pyth\xf6n.org")
        self.assertEqual(str(b"xn--pythn-mua.org.", "idna"), "pyth\xf6n.org.") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:7,代码来源:test_codecs.py

示例3: test_builtin_encode

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def test_builtin_encode(self):
        self.assertEqual("python.org".encode("idna"), b"python.org")
        self.assertEqual("python.org.".encode("idna"), b"python.org.")
        self.assertEqual("pyth\xf6n.org".encode("idna"), b"xn--pythn-mua.org")
        self.assertEqual("pyth\xf6n.org.".encode("idna"), b"xn--pythn-mua.org.") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:7,代码来源:test_codecs.py

示例4: test_stream

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def test_stream(self):
        r = codecs.getreader("idna")(io.BytesIO(b"abc"))
        r.read(3)
        self.assertEqual(r.read(), "") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:6,代码来源:test_codecs.py

示例5: test_incremental_decode

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def test_incremental_decode(self):
        self.assertEqual(
            "".join(codecs.iterdecode((bytes([c]) for c in b"python.org"), "idna")),
            "python.org"
        )
        self.assertEqual(
            "".join(codecs.iterdecode((bytes([c]) for c in b"python.org."), "idna")),
            "python.org."
        )
        self.assertEqual(
            "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")),
            "pyth\xf6n.org."
        )
        self.assertEqual(
            "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")),
            "pyth\xf6n.org."
        )

        decoder = codecs.getincrementaldecoder("idna")()
        self.assertEqual(decoder.decode(b"xn--xam", ), "")
        self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.")
        self.assertEqual(decoder.decode(b"rg"), "")
        self.assertEqual(decoder.decode(b"", True), "org")

        decoder.reset()
        self.assertEqual(decoder.decode(b"xn--xam", ), "")
        self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.")
        self.assertEqual(decoder.decode(b"rg."), "org.")
        self.assertEqual(decoder.decode(b"", True), "") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:31,代码来源:test_codecs.py

示例6: test_errors

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def test_errors(self):
        """Only supports "strict" error handler"""
        "python.org".encode("idna", "strict")
        b"python.org".decode("idna", "strict")
        for errors in ("ignore", "replace", "backslashreplace",
                "surrogateescape"):
            self.assertRaises(Exception, "python.org".encode, "idna", errors)
            self.assertRaises(Exception,
                b"python.org".decode, "idna", errors) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:11,代码来源:test_codecs.py

示例7: test_basics_capi

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def test_basics_capi(self):
        from _testcapi import codec_incrementalencoder, codec_incrementaldecoder
        s = "abc123"  # all codecs should be able to encode these
        for encoding in all_unicode_encodings:
            if encoding not in broken_unicode_with_stateful:
                # check incremental decoder/encoder (fetched via the C API)
                try:
                    cencoder = codec_incrementalencoder(encoding)
                except LookupError:  # no IncrementalEncoder
                    pass
                else:
                    # check C API
                    encodedresult = b""
                    for c in s:
                        encodedresult += cencoder.encode(c)
                    encodedresult += cencoder.encode("", True)
                    cdecoder = codec_incrementaldecoder(encoding)
                    decodedresult = ""
                    for c in encodedresult:
                        decodedresult += cdecoder.decode(bytes([c]))
                    decodedresult += cdecoder.decode(b"", True)
                    self.assertEqual(decodedresult, s,
                                     "encoding=%r" % encoding)

                if encoding not in ("idna", "mbcs"):
                    # check incremental decoder/encoder with errors argument
                    try:
                        cencoder = codec_incrementalencoder(encoding, "ignore")
                    except LookupError:  # no IncrementalEncoder
                        pass
                    else:
                        encodedresult = b"".join(cencoder.encode(c) for c in s)
                        cdecoder = codec_incrementaldecoder(encoding, "ignore")
                        decodedresult = "".join(cdecoder.decode(bytes([c]))
                                                for c in encodedresult)
                        self.assertEqual(decodedresult, s,
                                         "encoding=%r" % encoding) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:39,代码来源:test_codecs.py

示例8: test_seek

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def test_seek(self):
        # all codecs should be able to encode these
        s = "%s\n%s\n" % (100*"abc123", 100*"def456")
        for encoding in all_unicode_encodings:
            if encoding == "idna": # FIXME: See SF bug #1163178
                continue
            if encoding in broken_unicode_with_stateful:
                continue
            reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding)))
            for t in range(5):
                # Test that calling seek resets the internal codec state and buffers
                reader.seek(0, 0)
                data = reader.read()
                self.assertEqual(s, data) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:16,代码来源:test_codecs.py

示例9: test_bad_decode_args

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def test_bad_decode_args(self):
        for encoding in all_unicode_encodings:
            decoder = codecs.getdecoder(encoding)
            self.assertRaises(TypeError, decoder)
            if encoding not in ("idna", "punycode"):
                self.assertRaises(TypeError, decoder, 42) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:8,代码来源:test_codecs.py

示例10: validate

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def validate(self, value, record_id=None):
        if (
            not (isinstance(value, (basestring, unicodeT)))
            or not value
            or "@" not in value
        ):
            raise ValidationError(self.translator(self.error_message))

        body, domain = value.rsplit("@", 1)

        try:
            regex_flags = re.VERBOSE | re.IGNORECASE
            match_body = re.match(self.REGEX_BODY, body, regex_flags)
            match_domain = re.match(self.REGEX_DOMAIN, domain, regex_flags)

            if not match_domain:
                # check for Internationalized Domain Names
                # see https://docs.python.org/2/library/codecs.html#module-encodings.idna
                domain_encoded = to_unicode(domain).encode("idna").decode("ascii")
                match_domain = re.match(self.REGEX_DOMAIN, domain_encoded, regex_flags)

            match = (match_body is not None) and (match_domain is not None)
        except (TypeError, UnicodeError):
            # Value may not be a string where we can look for matches.
            # Example: we're calling ANY_OF formatter and IS_EMAIL is asked to validate a date.
            match = None
        if match:
            if (not self.banned or not self.banned.match(domain)) and (
                not self.forced or self.forced.match(domain)
            ):
                return value
        raise ValidationError(self.translator(self.error_message)) 
开发者ID:web2py,项目名称:pydal,代码行数:34,代码来源:validators.py

示例11: unicode_to_ascii_authority

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def unicode_to_ascii_authority(authority):
    """
    Follows the steps in RFC 3490, Section 4 to convert a unicode authority
    string into its ASCII equivalent.
    For example, u'www.Alliancefran\\xe7aise.nu' will be converted into
    'www.xn--alliancefranaise-npb.nu'

    Args:
        authority: unicode string, the URL authority component to convert,
            e.g. u'www.Alliancefran\\xe7aise.nu'

    Returns:
        string: the US-ASCII character equivalent to the inputed authority,
             e.g. 'www.xn--alliancefranaise-npb.nu'

    Raises:
        Exception: if the function is not able to convert the inputed
            authority

    @author: Jonathan Benn
    """
    # RFC 3490, Section 4, Step 1
    # The encodings.idna Python module assumes that AllowUnassigned == True

    # RFC 3490, Section 4, Step 2
    labels = re.split(REGEX_AUTHORITY_SPLITTER, authority)

    # RFC 3490, Section 4, Step 3
    # The encodings.idna Python module assumes that UseSTD3ASCIIRules == False

    # RFC 3490, Section 4, Step 4
    # We use the ToASCII operation because we are about to put the authority
    # into an IDN-unaware slot
    asciiLabels = []
    for label in labels:
        if label:
            asciiLabels.append(to_native(encodings.idna.ToASCII(label)))
        else:
            # encodings.idna.ToASCII does not accept an empty string, but
            # it is necessary for us to allow for empty labels so that we
            # don't modify the URL
            asciiLabels.append("")
    # RFC 3490, Section 4, Step 5
    return str(reduce(lambda x, y: x + unichr(0x002E) + y, asciiLabels)) 
开发者ID:web2py,项目名称:pydal,代码行数:46,代码来源:validators.py

示例12: valid_url

# 需要导入模块: import encodings [as 别名]
# 或者: from encodings import idna [as 别名]
def valid_url(self, value):
        match = self.URL_REGEX.match(value)
        if not match:
            return False
        url = match.groupdict()

        if url['scheme'].lower() not in self.schemes:
            return False
        if url['host6']:
            if IPv6Type.valid_ip(url['host6']):
                return url
            else:
                return False
        if url['host4']:
            return url

        try:
            hostname = url['hostn'].encode('ascii').decode('ascii')
        except UnicodeError:
            try:
                hostname = url['hostn'].encode('idna').decode('ascii')
            except UnicodeError:
                return False

        if hostname[-1] == '.':
            hostname = hostname[:-1]
        if len(hostname) > 253:
            return False

        labels = hostname.split('.')
        for label in labels:
            if not 0 < len(label) < 64:
                return False
            if '-' in (label[0], label[-1]):
                return False
        if self.fqdn:
            if len(labels) == 1 \
              or not self.TLD_REGEX.match(labels[-1]):
                return False

        url['hostn_enc'] = hostname

        return url 
开发者ID:splunk,项目名称:SA-ctf_scoreboard,代码行数:45,代码来源:net.py


注:本文中的encodings.idna方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。