Python encodings.idna.nameprep() Examples

The following are 21 code examples of encodings.idna.nameprep(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module encodings.idna , or try the search function .
Example #1
Source File: test_codecs.py    From BinderFilter with MIT License 6 votes vote down vote up
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 = unicode(orig, "utf-8")
            if prepped is None:
                # Input contains prohibited characters
                self.assertRaises(UnicodeError, nameprep, orig)
            else:
                prepped = unicode(prepped, "utf-8")
                try:
                    self.assertEqual(nameprep(orig), prepped)
                except Exception,e:
                    raise test_support.TestFailed("Test 3.%d: %s" % (pos+1, str(e))) 
Example #2
Source File: test_codecs.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
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 = unicode(orig, "utf-8")
            if prepped is None:
                # Input contains prohibited characters
                self.assertRaises(UnicodeError, nameprep, orig)
            else:
                prepped = unicode(prepped, "utf-8")
                try:
                    self.assertEqual(nameprep(orig), prepped)
                except Exception,e:
                    raise test_support.TestFailed("Test 3.%d: %s" % (pos+1, str(e))) 
Example #3
Source File: test_codecs.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
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 = unicode(orig, "utf-8")
            if prepped is None:
                # Input contains prohibited characters
                self.assertRaises(UnicodeError, nameprep, orig)
            else:
                prepped = unicode(prepped, "utf-8")
                try:
                    self.assertEqual(nameprep(orig), prepped)
                except Exception,e:
                    raise test_support.TestFailed("Test 3.%d: %s" % (pos+1, str(e))) 
Example #4
Source File: test_codecs.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
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 = unicode(orig, "utf-8")
            if prepped is None:
                # Input contains prohibited characters
                self.assertRaises(UnicodeError, nameprep, orig)
            else:
                prepped = unicode(prepped, "utf-8")
                try:
                    self.assertEquals(nameprep(orig), prepped)
                except Exception,e:
                    raise test_support.TestFailed("Test 3.%d: %s" % (pos+1, str(e))) 
Example #5
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
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))) 
Example #6
Source File: test_codecs.py    From gcblue with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 = unicode(orig, "utf-8")
            if prepped is None:
                # Input contains prohibited characters
                self.assertRaises(UnicodeError, nameprep, orig)
            else:
                prepped = unicode(prepped, "utf-8")
                try:
                    self.assertEqual(nameprep(orig), prepped)
                except Exception,e:
                    raise test_support.TestFailed("Test 3.%d: %s" % (pos+1, str(e))) 
Example #7
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
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))) 
Example #8
Source File: test_codecs.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
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))) 
Example #9
Source File: test_codecs.py    From oss-ftp with MIT License 6 votes vote down vote up
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 = unicode(orig, "utf-8")
            if prepped is None:
                # Input contains prohibited characters
                self.assertRaises(UnicodeError, nameprep, orig)
            else:
                prepped = unicode(prepped, "utf-8")
                try:
                    self.assertEqual(nameprep(orig), prepped)
                except Exception,e:
                    raise test_support.TestFailed("Test 3.%d: %s" % (pos+1, str(e))) 
Example #10
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
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 = unicode(orig, "utf-8")
            if prepped is None:
                # Input contains prohibited characters
                self.assertRaises(UnicodeError, nameprep, orig)
            else:
                prepped = unicode(prepped, "utf-8")
                try:
                    self.assertEqual(nameprep(orig), prepped)
                except Exception,e:
                    raise test_support.TestFailed("Test 3.%d: %s" % (pos+1, str(e))) 
Example #11
Source File: xmpp_stringprep.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def nameprep(self, label):
        label = idna.nameprep(label)
        self.check_prohibiteds(label)
        if label[0] == u'-':
            raise UnicodeError("Invalid leading hyphen-minus")
        if label[-1] == u'-':
            raise UnicodeError("Invalid trailing hyphen-minus")
        return label 
Example #12
Source File: xmpp_stringprep.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def prepare(self, string):
        result = []

        labels = idna.dots.split(string)

        if labels and len(labels[-1]) == 0:
            trailing_dot = u'.'
            del labels[-1]
        else:
            trailing_dot = u''

        for label in labels:
            result.append(self.nameprep(label))

        return u".".join(result) + trailing_dot 
Example #13
Source File: xmpp_stringprep.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def nameprep(self, label):
            return label.lower() 
Example #14
Source File: xmpp_stringprep.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def prepare(self, string):
        result = []

        labels = idna.dots.split(string)

        if labels and len(labels[-1]) == 0:
            trailing_dot = '.'
            del labels[-1]
        else:
            trailing_dot = ''

        for label in labels:
            result.append(self.nameprep(label))

        return ".".join(result) + trailing_dot 
Example #15
Source File: xmpp_stringprep.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def nameprep(self, label):
        label = idna.nameprep(label)
        self.check_prohibiteds(label)
        if label[0] == '-':
            raise UnicodeError, "Invalid leading hyphen-minus"
        if label[-1] == '-':
            raise UnicodeError, "Invalid trailing hyphen-minus"
        return label 
Example #16
Source File: jid.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def __prepare_domain(data):
        """Prepare domainpart of the JID.

        :Parameters:
            - `data`: Domain part of the JID
        :Types:
            - `data`: `str`

        :raise JIDError: if the domain name is too long."""
        if not data:
            raise JIDError("Domain must be given")
        data = data.rstrip(".")
        if not data:
            raise JIDError("Domain must be given")
        if '[' in data:
            if data[0] == '[' and data[-1] == ']':
                try:
                    # decode...
                    addr = socket.inet_pton(socket.AF_INET6, data[1:-1])
                    # ...and normalize
                    return "[{0}]".format(
                                    socket.inet_ntop(socket.AF_INET6, addr))
                except socket.error:
                    raise JIDError("Invalid IPv6 literal in JID domainpart")
            else:
                raise JIDError("Invalid use of '[' or ']' in JID domainpart")
        elif data[0].isdigit() and data[-1].isdigit():
            try:
                # try to decode as IPv4...
                addr = socket.inet_pton(socket.AF_INET, data)
                # ...and normalize
                return socket.inet_ntop(socket.AF_INET, addr)
            except socket.error:
                pass
        data = str(data)
        labels = data.split(".")
        labels = [idna.nameprep(label) for label in labels]
        domain = ".".join(labels)
        if len(domain.encode("utf-8")) > 1023:
            raise JIDError("Domain name too long")
        return domain 
Example #17
Source File: xmpp_stringprep.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def nameprep(self, label):
        label = idna.nameprep(label)
        self.check_prohibiteds(label)
        if label[0] == u'-':
            raise UnicodeError("Invalid leading hyphen-minus")
        if label[-1] == u'-':
            raise UnicodeError("Invalid trailing hyphen-minus")
        return label 
Example #18
Source File: xmpp_stringprep.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def prepare(self, string):
        result = []

        labels = idna.dots.split(string)

        if labels and len(labels[-1]) == 0:
            trailing_dot = u'.'
            del labels[-1]
        else:
            trailing_dot = u''

        for label in labels:
            result.append(self.nameprep(label))

        return u".".join(result) + trailing_dot 
Example #19
Source File: xmpp_stringprep.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def nameprep(self, label):
            return label.lower() 
Example #20
Source File: xmpp_stringprep.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def prepare(self, string):
        result = []

        labels = idna.dots.split(string)

        if labels and len(labels[-1]) == 0:
            trailing_dot = '.'
            del labels[-1]
        else:
            trailing_dot = ''

        for label in labels:
            result.append(self.nameprep(label))

        return ".".join(result) + trailing_dot 
Example #21
Source File: xmpp_stringprep.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def nameprep(self, label):
        label = idna.nameprep(label)
        self.check_prohibiteds(label)
        if label[0] == '-':
            raise UnicodeError, "Invalid leading hyphen-minus"
        if label[-1] == '-':
            raise UnicodeError, "Invalid trailing hyphen-minus"
        return label