本文整理汇总了Python中socket.IPPROTO_IP属性的典型用法代码示例。如果您正苦于以下问题:Python socket.IPPROTO_IP属性的具体用法?Python socket.IPPROTO_IP怎么用?Python socket.IPPROTO_IP使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.IPPROTO_IP属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _create_ipv4_sockets
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def _create_ipv4_sockets(loopback_enabled):
# Open a multicast send socket, with IP_MULTICAST_LOOP enabled or disabled as requested.
mcast_address = "224.0.1.195"
port = 49501
group = (mcast_address, port)
txsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
txsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if loopback_enabled:
txsock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
else:
txsock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)
txsock.connect(group)
# Open a multicast receive socket and join the group
rxsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
req = struct.pack("=4sl", socket.inet_aton(mcast_address), socket.INADDR_ANY)
rxsock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, req)
rxsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
rxsock.bind(group)
return (txsock, rxsock)
示例2: _do_dns_lookup
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def _do_dns_lookup(hostname: str, port: int) -> str:
try:
addr_infos = socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.IPPROTO_IP)
except (socket.gaierror, IndexError, ConnectionError):
raise ServerHostnameCouldNotBeResolved(f"Could not resolve {hostname}")
family, socktype, proto, canonname, sockaddr = addr_infos[0]
# By default use the first DNS entry, IPv4 or IPv6
tentative_ip_addr = sockaddr[0]
# But try to use IPv4 if we have both IPv4 and IPv6 addresses, to work around buggy networks
for family, socktype, proto, canonname, sockaddr in addr_infos:
if family == socket.AF_INET:
tentative_ip_addr = sockaddr[0]
return tentative_ip_addr
示例3: run
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def run(self, ttl=None):
if self.DISABLE_SSDP_LISTENER:
return
self.allow_reuse_address = True
SocketServer.UDPServer.__init__(
self, (self.host or '', self.SSDP_PORT), SSDPHandler)
self.socket.setsockopt(
socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
self._multicast_struct(self.SSDP_ADDRESS))
self.socket.setsockopt(
socket.IPPROTO_IP,
socket.IP_MULTICAST_TTL,
self.SSDP_TTL)
if ttl:
GObject.timeout_add(ttl * 1000, self.shutdown)
setproctitle.setproctitle('ssdp_listener')
self.serve_forever(self)
logger.info('SSDPListener.run()')
示例4: activate_network_interface
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def activate_network_interface(iface):
"""Bring up the given network interface.
@raise OSError: if interface does not exist or permissions are missing
"""
iface = iface.encode()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP)
try:
# Get current interface flags from kernel
ifreq = struct.pack(
_STRUCT_IFREQ_LAYOUT_IFADDR_SAFAMILY, iface, socket.AF_INET, b"0" * 14
)
ifreq = fcntl.ioctl(sock, _SIOCGIFFLAGS, ifreq)
if_flags = struct.unpack(_STRUCT_IFREQ_LAYOUT_IFFLAGS, ifreq)[1]
# Set new flags
ifreq = struct.pack(
_STRUCT_IFREQ_LAYOUT_IFFLAGS, iface, if_flags | _IFF_UP, b"0" * 14
)
fcntl.ioctl(sock, _SIOCSIFFLAGS, ifreq)
finally:
sock.close()
示例5: ifconfig
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def ifconfig(dev, ipaddr, netmask):
# http://stackoverflow.com/questions/6652384/how-to-set-the-ip-address-from-c-in-linux
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP)
AF_INET = socket.AF_INET
fd = sock.fileno()
addrbuf = struct.pack('BBBB', *[int(el) for el in ipaddr.split('.')])
maskbuf = struct.pack('BBBB', *[int(el) for el in netmask.split('.')])
sockaddr_mt = '16sHH4s'
flags_mt = '16sH'
# ADDR
siocsifaddr = struct.pack(sockaddr_mt, dev, AF_INET, 0, addrbuf)
fcntl.ioctl(fd, SIOCSIFADDR, siocsifaddr)
# MASK
siocsifnetmask = struct.pack(sockaddr_mt, dev, AF_INET, 0, maskbuf)
fcntl.ioctl(fd, SIOCSIFNETMASK, siocsifnetmask)
# ifconfig tun0 up
ifr2 = struct.pack(flags_mt, dev, 0)
ifr_ret = fcntl.ioctl(fd, SIOCGIFFLAGS, ifr2)
cur_flags = struct.unpack(flags_mt, ifr_ret)[1]
flags = cur_flags | (IFF_UP | IFF_RUNNING)
ifr_ret = struct.pack(flags_mt, dev, flags)
ifr_ret = fcntl.ioctl(fd, SIOCSIFFLAGS, ifr_ret)
return 0
示例6: receive
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def receive(*, group=DEFAULT_GROUP, port=DEFAULT_PORT, timeout=None):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
# not available on Windows
pass
s.bind(('', port))
s.settimeout(timeout)
mreq = struct.pack('4sl', socket.inet_aton(group), socket.INADDR_ANY)
s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
try:
data, sender_addr = s.recvfrom(4096)
finally:
s.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, mreq)
finally:
s.close()
return data, sender_addr
示例7: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def __init__(self, summary_table, group=DEFAULT_GROUP, port=DEFAULT_PORT, timeout=None):
self._dummy_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
try:
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
# not available on Windows
pass
self._socket.bind(('', port))
self._socket.settimeout(timeout)
self._mreq = struct.pack('4sl', socket.inet_aton(group), socket.INADDR_ANY)
self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, self._mreq)
except Exception:
self._dummy_socket.close()
self._socket.close()
raise
self._is_shutdown = False
self._summary_table = summary_table
self._group = group
self._port = port
示例8: _socketpair_compat
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def _socketpair_compat():
"""TCP/IP socketpair including Windows support"""
listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
listensock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listensock.bind(("127.0.0.1", 0))
listensock.listen(1)
iface, port = listensock.getsockname()
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
sock1.setblocking(0)
try:
sock1.connect(("localhost", port))
except socket.error as err:
if err.errno != errno.EINPROGRESS and err.errno != errno.EWOULDBLOCK and err.errno != EAGAIN:
raise
sock2, address = listensock.accept()
sock2.setblocking(0)
listensock.close()
return (sock1, sock2)
示例9: _init_advertiser
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def _init_advertiser(self):
sock = _find_sock()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, struct.pack('@I', 1))
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, struct.pack('@I', 1))
if sock.family == socket.AF_INET6:
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, struct.pack('@I', 1))
sock.bind(('', self.port))
addrs = _resolve_addrs(self.addresses, None, self.ignore_unavailable, (sock.family,))
for fam,to,orig_fam,orig_addr in addrs:
try:
_multicast_join_group(sock, orig_fam, orig_addr)
except socket.error:
if not self.ignore_unavailable:
raise
self._sock = sock
示例10: check_udp_connectivity
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def check_udp_connectivity(url, timeout=None):
"""Check UDP connectivity."""
ipaddr, port = url.rsplit(':', 1)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.settimeout(timeout)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(('', int(port)))
mreq = struct.pack("4sl", socket.inet_aton(ipaddr), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
try:
if sock.recv(10240):
return True
except socket.timeout:
pass
return False
示例11: test_close
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def test_close(self):
proc = psutil.Process()
ip_addresses = {info[4][0] for info in socket.getaddrinfo(
'example.com', 80, socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP
)}
self.assertGreater(len(ip_addresses), 0)
protocol = Protocol(config=Configuration(
service_endpoint='http://example.com', credentials=Credentials('A', 'B'),
auth_type=NOAUTH, version=Version(Build(15, 1)), retry_policy=FailFast()
))
session = protocol.get_session()
session.get('http://example.com')
self.assertEqual(len({p.raddr[0] for p in proc.connections() if p.raddr[0] in ip_addresses}), 1)
protocol.release_session(session)
protocol.close()
self.assertEqual(len({p.raddr[0] for p in proc.connections() if p.raddr[0] in ip_addresses}), 0)
示例12: _create_mcast_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def _create_mcast_socket(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if self._interface != 'any':
if platform.system() != "Windows":
sock.bind((self.MULTICAST_ADDRESS, self.MULTICAST_PORT))
else:
sock.bind((self._interface, self.MULTICAST_PORT))
mreq = socket.inet_aton(self.MULTICAST_ADDRESS) + socket.inet_aton(self._interface)
else:
if platform.system() != "Windows":
sock.bind((self.MULTICAST_ADDRESS, self.MULTICAST_PORT))
else:
sock.bind(('', self.MULTICAST_PORT))
mreq = struct.pack("=4sl", socket.inet_aton(self.MULTICAST_ADDRESS), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
return sock
示例13: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def __init__(self, config, event_loop):
main_protocol = MainProtocol(config)
task = event_loop.create_datagram_endpoint(lambda: main_protocol,
local_addr=(config['ip'], 9898))
asyncio.ensure_future(task, loop=event_loop)
sock = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('224.0.0.50', 4321))
mreq = socket.inet_aton('224.0.0.50') + socket.inet_aton(config['ip'])
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
discovery_protocol = DiscoveryProtocol(config, main_protocol)
task = event_loop.create_datagram_endpoint(lambda: discovery_protocol,
sock=sock)
asyncio.ensure_future(task, loop=event_loop)
self.discovery_protocol = discovery_protocol
self.main_protocol = main_protocol
示例14: _init_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def _init_socket(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 10)
sock.bind((self._root.bind_ip, COAP_PORT))
if self._root.host_ip:
mreq = struct.pack("=4s4s",
socket.inet_aton(COAP_IP),
socket.inet_aton(self._root.host_ip))
else:
mreq = struct.pack("=4sl",
socket.inet_aton(COAP_IP),
socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
sock.settimeout(15)
self._socket = sock
示例15: _socketpair_compat
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPPROTO_IP [as 别名]
def _socketpair_compat():
"""TCP/IP socketpair including Windows support"""
listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
listensock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listensock.bind(("127.0.0.1", 0))
listensock.listen(1)
iface, port = listensock.getsockname()
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
sock1.setblocking(0)
try:
sock1.connect(("127.0.0.1", port))
except socket.error as err:
if err.errno != errno.EINPROGRESS and err.errno != errno.EWOULDBLOCK and err.errno != EAGAIN:
raise
sock2, address = listensock.accept()
sock2.setblocking(0)
listensock.close()
return (sock1, sock2)