本文整理匯總了Python中socket.NI_DGRAM屬性的典型用法代碼示例。如果您正苦於以下問題:Python socket.NI_DGRAM屬性的具體用法?Python socket.NI_DGRAM怎麽用?Python socket.NI_DGRAM使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類socket
的用法示例。
在下文中一共展示了socket.NI_DGRAM屬性的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _getnameinfo
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import NI_DGRAM [as 別名]
def _getnameinfo(sockaddr, flags=0):
host = sockaddr[0]
port = sockaddr[1]
if len(sockaddr) == 4:
scope = sockaddr[3]
family = socket.AF_INET6
else:
scope = None
family = socket.AF_INET
tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
socket.SOL_TCP, 0)
if len(tuples) > 1:
raise socket.error('sockaddr resolved to multiple addresses')
addr = tuples[0][4][0]
if flags & socket.NI_DGRAM:
pname = 'udp'
else:
pname = 'tcp'
qname = dns.reversename.from_address(addr)
if flags & socket.NI_NUMERICHOST == 0:
try:
answer = _resolver.query(qname, 'PTR')
hostname = answer.rrset[0].target.to_text(True)
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
if flags & socket.NI_NAMEREQD:
raise socket.gaierror(socket.EAI_NONAME)
hostname = addr
if scope is not None:
hostname += '%' + str(scope)
else:
hostname = addr
if scope is not None:
hostname += '%' + str(scope)
if flags & socket.NI_NUMERICSERV:
service = str(port)
else:
service = socket.getservbyport(port, pname)
return (hostname, service)
示例2: testPort
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import NI_DGRAM [as 別名]
def testPort(self):
for address, flags, expected in [
( ("127.0.0.1", 25), 0, "smtp" ),
( ("127.0.0.1", 25), socket.NI_NUMERICSERV, 25 ),
( ("127.0.0.1", 513), socket.NI_DGRAM, "who" ),
( ("127.0.0.1", 513), 0, "login"),
]:
result = socket.getnameinfo(address, flags)
self.failUnlessEqual(result[1], expected)
示例3: _getnameinfo
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import NI_DGRAM [as 別名]
def _getnameinfo(sockaddr, flags=0):
host = sockaddr[0]
port = sockaddr[1]
if len(sockaddr) == 4:
scope = sockaddr[3]
family = socket.AF_INET6
else:
scope = None
family = socket.AF_INET
tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
socket.SOL_TCP, 0)
if len(tuples) > 1:
raise socket.error('sockaddr resolved to multiple addresses')
addr = tuples[0][4][0]
if flags & socket.NI_DGRAM:
pname = 'udp'
else:
pname = 'tcp'
qname = thirdparty.dns.reversename.from_address(addr)
if flags & socket.NI_NUMERICHOST == 0:
try:
answer = _resolver.query(qname, 'PTR')
hostname = answer.rrset[0].target.to_text(True)
except (thirdparty.dns.resolver.NXDOMAIN, thirdparty.dns.resolver.NoAnswer):
if flags & socket.NI_NAMEREQD:
raise socket.gaierror(socket.EAI_NONAME)
hostname = addr
if scope is not None:
hostname += '%' + str(scope)
else:
hostname = addr
if scope is not None:
hostname += '%' + str(scope)
if flags & socket.NI_NUMERICSERV:
service = str(port)
else:
service = socket.getservbyport(port, pname)
return (hostname, service)
示例4: testPort
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import NI_DGRAM [as 別名]
def testPort(self):
for address, flags, expected in [
( ("127.0.0.1", 25), 0, "smtp" ),
( ("127.0.0.1", 25), socket.NI_NUMERICSERV, 25 ),
# This portion of the test does not suceed on OS X;
# the above entries probably suffice
# ( ("127.0.0.1", 513), socket.NI_DGRAM, "who" ),
# ( ("127.0.0.1", 513), 0, "login"),
]:
result = socket.getnameinfo(address, flags)
self.failUnlessEqual(result[1], expected)
# This test currently fails due to the recent changes (as of March 2014) at python.org:
# TBD perhaps there are well-known addresses that guarantee stable resolution
# def testHost(self):
# for address, flags, expected in [
# ( ("www.python.org", 80), 0, "dinsdale.python.org"),
# ( ("www.python.org", 80), socket.NI_NUMERICHOST, "82.94.164.162" ),
# ( ("www.python.org", 80), socket.NI_NAMEREQD, "dinsdale.python.org"),
# ( ("82.94.164.162", 80), socket.NI_NAMEREQD, "dinsdale.python.org"),
# ]:
# result = socket.getnameinfo(address, flags)
# self.failUnlessEqual(result[0], expected)