本文整理匯總了Python中encodings.idna.nameprep方法的典型用法代碼示例。如果您正苦於以下問題:Python idna.nameprep方法的具體用法?Python idna.nameprep怎麽用?Python idna.nameprep使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類encodings.idna
的用法示例。
在下文中一共展示了idna.nameprep方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_nameprep
# 需要導入模塊: from encodings import idna [as 別名]
# 或者: from encodings.idna import nameprep [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 = 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)))
示例2: test_nameprep
# 需要導入模塊: from encodings import idna [as 別名]
# 或者: from encodings.idna import nameprep [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)))
示例3: test_nameprep
# 需要導入模塊: from encodings import idna [as 別名]
# 或者: from encodings.idna import nameprep [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 = 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)))
示例4: prepare
# 需要導入模塊: from encodings import idna [as 別名]
# 或者: from encodings.idna import nameprep [as 別名]
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
示例5: nameprep
# 需要導入模塊: from encodings import idna [as 別名]
# 或者: from encodings.idna import nameprep [as 別名]
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
示例6: nameprep
# 需要導入模塊: from encodings import idna [as 別名]
# 或者: from encodings.idna import nameprep [as 別名]
def nameprep(self, label):
return label.lower()
示例7: prepare
# 需要導入模塊: from encodings import idna [as 別名]
# 或者: from encodings.idna import nameprep [as 別名]
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
示例8: __prepare_domain
# 需要導入模塊: from encodings import idna [as 別名]
# 或者: from encodings.idna import nameprep [as 別名]
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