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)
python类NI_NUMERICHOST的实例源码
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)
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)
def __init__(self, host, port):
host, port = socket.getnameinfo((host, port), socket.NI_NUMERICHOST)
self.addr = (host, int(port))
self._path = "%s:%s" % (host, port)
self.socket = None
self._create_socket()
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)
def test_getnameinfo():
# Trivial test:
ni_numeric = stdlib_socket.NI_NUMERICHOST | stdlib_socket.NI_NUMERICSERV
with assert_checkpoints():
got = await tsocket.getnameinfo(("127.0.0.1", 1234), ni_numeric)
assert got == ("127.0.0.1", "1234")
# getnameinfo requires a numeric address as input:
with assert_checkpoints():
with pytest.raises(tsocket.gaierror):
await tsocket.getnameinfo(("google.com", 80), 0)
with assert_checkpoints():
with pytest.raises(tsocket.gaierror):
await tsocket.getnameinfo(("localhost", 80), 0)
# Blocking call to get expected values:
host, service = stdlib_socket.getnameinfo(("127.0.0.1", 80), 0)
# Some working calls:
got = await tsocket.getnameinfo(("127.0.0.1", 80), 0)
assert got == (host, service)
got = await tsocket.getnameinfo(("127.0.0.1", 80), tsocket.NI_NUMERICHOST)
assert got == ("127.0.0.1", service)
got = await tsocket.getnameinfo(("127.0.0.1", 80), tsocket.NI_NUMERICSERV)
assert got == (host, "80")
################################################################
# constructors
################################################################
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)
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)
resolver.py 文件源码
项目:Infrax-as-Code-1000-webservers-in-40-minutes
作者: ezeeetm
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
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)
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)
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)
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)
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 = 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 (resolver.NXDOMAIN, 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)
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)
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)
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)
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)