本文整理汇总了Python中socket.AF_INET6属性的典型用法代码示例。如果您正苦于以下问题:Python socket.AF_INET6属性的具体用法?Python socket.AF_INET6怎么用?Python socket.AF_INET6使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.AF_INET6属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def __init__(self, laddress, data_callback, family = None, o = None):
if o == None:
if family == None:
if laddress != None and laddress[0].startswith('['):
family = socket.AF_INET6
laddress = (laddress[0][1:-1], laddress[1])
else:
family = socket.AF_INET
self.family = family
self.laddress = laddress
self.data_callback = data_callback
else:
self.laddress, self.data_callback, self.family, self.nworkers, self.flags, \
self.ploss_out_rate, self.pdelay_out_max, self.ploss_in_rate, \
self.pdelay_in_max = o.laddress, o.data_callback, o.family, \
o.nworkers, o.flags, o.ploss_out_rate, o.pdelay_out_max, o.ploss_in_rate, \
o.pdelay_in_max
示例2: send_to
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def send_to(self, data, address, delayed = False):
if not isinstance(address, tuple):
raise Exception('Invalid address, not a tuple: %s' % str(address))
if not isinstance(data, bytes):
data = data.encode('utf-8')
if self.uopts.ploss_out_rate > 0.0 and not delayed:
if random() < self.uopts.ploss_out_rate:
return
if self.uopts.pdelay_out_max > 0.0 and not delayed:
pdelay = self.uopts.pdelay_out_max * random()
Timeout(self.send_to, pdelay, 1, data, address, True)
return
addr, port = address
if self.uopts.family == socket.AF_INET6:
if not addr.startswith('['):
raise Exception('Invalid IPv6 address: %s' % addr)
address = (addr[1:-1], port)
self.wi_available.acquire()
self.wi.append((data, address))
self.wi_available.notify()
self.wi_available.release()
示例3: bind_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def bind_socket(host: str, port: int, *, backlog=100) -> socket.socket:
"""Create TCP server socket.
:param host: IPv4, IPv6 or hostname may be specified
:param port: TCP port number
:param backlog: Maximum number of connections to queue
:return: socket.socket object
"""
try: # IP address: family must be specified for IPv6 at least
ip = ip_address(host)
host = str(ip)
sock = socket.socket(
socket.AF_INET6 if ip.version == 6 else socket.AF_INET
)
except ValueError: # Hostname, may become AF_INET or AF_INET6
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(backlog)
return sock
示例4: is_ipaddress
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def is_ipaddress(hostname):
"""Detects whether the hostname given is an IP address.
:param str hostname: Hostname to examine.
:return: True if the hostname is an IP address, False otherwise.
"""
if six.PY3 and isinstance(hostname, bytes):
# IDN A-label bytes are ASCII compatible.
hostname = hostname.decode('ascii')
families = [socket.AF_INET]
if hasattr(socket, 'AF_INET6'):
families.append(socket.AF_INET6)
for af in families:
try:
inet_pton(af, hostname)
except (socket.error, ValueError, OSError):
pass
else:
return True
return False
示例5: kernel_route_dst_prefix_str
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def kernel_route_dst_prefix_str(route):
dst = route.get_attr('RTA_DST')
if dst is None:
family = route["family"]
if family == socket.AF_INET:
prefix_str = "0.0.0.0/0"
elif family == socket.AF_INET6:
prefix_str = "::/0"
else:
prefix_str = "Default"
else:
prefix_str = dst
dst_len = route["dst_len"]
if dst_len is not None:
prefix_str += "/" + str(dst_len)
return prefix_str
示例6: log_tx_protocol_packet
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def log_tx_protocol_packet(self, level, sock, prelude, packet_info):
if not self._tx_log.isEnabledFor(level):
return
if sock.family == socket.AF_INET:
fam_str = "IPv4"
from_str = "from {}:{}".format(sock.getsockname()[0], sock.getsockname()[1])
to_str = "to {}:{}".format(sock.getpeername()[0], sock.getpeername()[1])
else:
assert sock.family == socket.AF_INET6
fam_str = "IPv6"
from_str = "from [{}]:{}".format(sock.getsockname()[0], sock.getsockname()[1])
to_str = "to [{}]:{}".format(sock.getpeername()[0], sock.getpeername()[1])
type_str = self.protocol_packet_type(packet_info.protocol_packet)
packet_str = str(packet_info)
self._tx_log.log(level, "[%s] %s %s %s %s %s %s" %
(self._log_id, prelude, fam_str, type_str, from_str, to_str, packet_str))
示例7: create_socket_ipv6_tx_ucast
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def create_socket_ipv6_tx_ucast(self, remote_address, port):
try:
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
except IOError as err:
self.warning("Could not create IPv6 UDP socket: %s", err)
return None
self.enable_addr_and_port_reuse(sock)
try:
sock_addr = socket.getaddrinfo(remote_address, port, socket.AF_INET6,
socket.SOCK_DGRAM)[0][4]
sock.connect(sock_addr)
except IOError as err:
self.warning("Could not connect UDP socket to address %s port %d: %s",
remote_address, port, err)
return None
return sock
示例8: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def __init__(self, stream, address, protocol):
self.address = address
# Save the socket's address family now so we know how to
# interpret self.address even after the stream is closed
# and its socket attribute replaced with None.
if stream.socket is not None:
self.address_family = stream.socket.family
else:
self.address_family = None
# In HTTPServerRequest we want an IP, not a full socket address.
if (self.address_family in (socket.AF_INET, socket.AF_INET6) and
address is not None):
self.remote_ip = address[0]
else:
# Unix (or other) socket; fake the remote address.
self.remote_ip = '0.0.0.0'
if protocol:
self.protocol = protocol
elif isinstance(stream, iostream.SSLIOStream):
self.protocol = "https"
else:
self.protocol = "http"
self._orig_remote_ip = self.remote_ip
self._orig_protocol = self.protocol
示例9: test_ipv6
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def test_ipv6(self):
try:
[sock] = bind_sockets(None, '::1', family=socket.AF_INET6)
port = sock.getsockname()[1]
self.http_server.add_socket(sock)
except socket.gaierror as e:
if e.args[0] == socket.EAI_ADDRFAMILY:
# python supports ipv6, but it's not configured on the network
# interface, so skip this test.
return
raise
url = '%s://[::1]:%d/hello' % (self.get_protocol(), port)
# ipv6 is currently enabled by default but can be disabled
self.http_client.fetch(url, self.stop, allow_ipv6=False)
response = self.wait()
self.assertEqual(response.code, 599)
self.http_client.fetch(url, self.stop)
response = self.wait()
self.assertEqual(response.body, b"Hello world!")
示例10: bind
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def bind(self, port, address=None, family=socket.AF_UNSPEC, backlog=128):
u"""绑定该服务到指定的地址的指定端口上.
要启动该服务, 调用 `start`. 如果你想要在一个单进程上运行该服务,
你可以调用 `listen` 作为顺序调用 `bind` 和 `start` 的一个快捷方式.
address 参数可以是 IP 地址或者主机名. 如果它是主机名,
该服务将监听在和该名称有关的所有 IP 地址上. 地址也可以是空字符串或者
None, 服务将监听所有可用的接口. family 可以被设置为 `socket.AF_INET` 或
`socket.AF_INET6` 用来限定是 IPv4 或 IPv6 地址, 否则如果可用的话, 两者
都将被使用.
``backlog`` 参数和 `socket.listen <socket.socket.listen>` 是相同含义.
这个方法可能在 `start` 之前被调用多次来监听在多个端口或接口上.
"""
sockets = bind_sockets(port, address=address, family=family,
backlog=backlog)
if self._started:
self.add_sockets(sockets)
else:
self._pending_sockets.extend(sockets)
示例11: domain_ip_parser
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def domain_ip_parser(ip_or_domain_or_url, local_dns, ipv6):
"""parsing the arg to get the hostname to query."""
#ip_or_domain_or_url = str(sys.argv[1])
if ip_or_domain_or_url.startswith("https://") or ip_or_domain_or_url.startswith("http://"):
ip_or_domain = ip_or_domain_or_url.split('/')[2]
elif ip_or_domain_or_url == ".":
ip_or_domain = ""
else:
ip_or_domain = ip_or_domain_or_url
if local_dns:
import socket
ip_or_domain = socket.gethostbyname(ip_or_domain)
elif ipv6:
import socket
ip_or_domain = socket.getaddrinfo(ip_or_domain, None, socket.AF_INET6)[0][-1][0]
return ip_or_domain
示例12: select_address_family
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def select_address_family(host, port):
"""Return ``AF_INET4``, ``AF_INET6``, or ``AF_UNIX`` depending on
the host and port."""
# disabled due to problems with current ipv6 implementations
# and various operating systems. Probably this code also is
# not supposed to work, but I can't come up with any other
# ways to implement this.
# try:
# info = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
# socket.SOCK_STREAM, 0,
# socket.AI_PASSIVE)
# if info:
# return info[0][0]
# except socket.gaierror:
# pass
if host.startswith("unix://"):
return socket.AF_UNIX
elif ":" in host and hasattr(socket, "AF_INET6"):
return socket.AF_INET6
return socket.AF_INET
示例13: bind_unused_port
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def bind_unused_port(sock: socket.socket, host: Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address] = 'localhost') -> int:
"""Bind the socket to a free port and return the port number.
This code is based on the code in the stdlib's test.test_support module."""
if sock.family in (socket.AF_INET, socket.AF_INET6) and sock.type == socket.SOCK_STREAM:
if hasattr(socket, "SO_EXCLUSIVEADDRUSE"):
with contextlib.suppress(socket.error):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
if not isinstance(host, str):
host = str(host)
if sock.family == socket.AF_INET:
if host == 'localhost':
sock.bind(('127.0.0.1', 0))
else:
sock.bind((host, 0))
elif sock.family == socket.AF_INET6:
if host == 'localhost':
sock.bind(('::1', 0, 0, 0))
else:
sock.bind((host, 0, 0, 0))
else:
raise CommunicationError("unsupported socket family: " + str(sock.family))
return sock.getsockname()[1]
示例14: start_ns
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def start_ns(host=None, port=None, enableBroadcast=True, bchost=None, bcport=None,
unixsocket=None, nathost=None, natport=None, storage=None):
"""utility fuction to quickly get a Name server daemon to be used in your own event loops.
Returns (nameserverUri, nameserverDaemon, broadcastServer)."""
daemon = NameServerDaemon(host, port, unixsocket, nathost=nathost, natport=natport, storage=storage)
bcserver = None
nsUri = daemon.uriFor(daemon.nameserver)
if not unixsocket:
hostip = daemon.sock.getsockname()[0]
if hostip.startswith("127."):
# not starting broadcast server for localhost.
enableBroadcast = False
if enableBroadcast:
internalUri = daemon.uriFor(daemon.nameserver, nat=False)
bcserver = BroadcastServer(internalUri, bchost, bcport, ipv6=daemon.sock.family == socket.AF_INET6)
return nsUri, daemon, bcserver
示例15: testCreateUnboundSockets6
# 需要导入模块: import socket [as 别名]
# 或者: from socket import AF_INET6 [as 别名]
def testCreateUnboundSockets6(self):
if not has_ipv6:
pytest.skip("no ipv6 capability")
s = socketutil.create_socket(ipv6=True)
assert socket.AF_INET6 == s.family
bs = socketutil.create_bc_socket(ipv6=True)
assert socket.AF_INET6 == bs.family
with contextlib.suppress(socket.error):
host, port, _, _ = s.getsockname()
# can either fail with socket.error or return (host,0)
assert 0 == port
with contextlib.suppress(socket.error):
host, port, _, _ = bs.getsockname()
# can either fail with socket.error or return (host,0)
assert 0 == port
s.close()
bs.close()