本文整理匯總了Python中socket.INADDR_ANY屬性的典型用法代碼示例。如果您正苦於以下問題:Python socket.INADDR_ANY屬性的具體用法?Python socket.INADDR_ANY怎麽用?Python socket.INADDR_ANY使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類socket
的用法示例。
在下文中一共展示了socket.INADDR_ANY屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _create_ipv4_sockets
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [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: receive
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [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
示例3: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [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
示例4: check_udp_connectivity
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [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
示例5: _create_mcast_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [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
示例6: _init_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [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
示例7: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [as 別名]
def __init__(self, group="239.255.255.250", port=1900):
self.group, self.port = group, port
self.callbacks = {}
# multicast socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
logger.debug("[SSDP] bind: 0.0.0.0:%s" % port)
sock.bind(('0.0.0.0', port))
mreq = struct.pack("=4sl", socket.inet_aton(group), socket.INADDR_ANY)
logger.debug("[SSDP] add membership: UDP/%s" % group)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
self.listening = False
self.sock = sock
self.devices = {}
# Start listening
示例8: handle_membership
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [as 別名]
def handle_membership(self, cmd):
if self._listen_interfaces is None:
mreq = struct.pack(
str("4sI"), socket.inet_aton(self._multicast_address[0]),
socket.INADDR_ANY)
self.socket.setsockopt(socket.IPPROTO_IP,
cmd, mreq)
else:
for interface in self._listen_interfaces:
try:
if_addr = socket.inet_aton(interface)
except socket.error:
if_addr = get_interface_address(interface)
mreq = socket.inet_aton(self._multicast_address[0]) + if_addr
self.socket.setsockopt(socket.IPPROTO_IP,
cmd, mreq)
示例9: create_sockets
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [as 別名]
def create_sockets(mcst_ipaddr, mcst_port):
mreq = struct.pack("4sl", socket.inet_aton(mcst_ipaddr),
socket.INADDR_ANY)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
if platform.system() == "Windows":
s.bind(("", mcst_port))
return (s, )
else:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
s.bind((mcst_ipaddr, mcst_port))
bsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
bsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
bsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
bsock.bind(("", 1901))
return (s, bsock)
示例10: init_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [as 別名]
def init_socket(self):
ok = True
self.ip = '239.255.255.250'
self.port = 1900
try:
#This is needed to join a multicast group
self.mreq = struct.pack("4sl",socket.inet_aton(self.ip),socket.INADDR_ANY)
#Set up server socket
self.ssock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,socket.IPPROTO_UDP)
self.ssock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
try:
self.ssock.bind(('',self.port))
except Exception, e:
dbg("WARNING: Failed to bind %s:%d: %s" , (self.ip,self.port,e))
ok = False
try:
self.ssock.setsockopt(socket.IPPROTO_IP,socket.IP_ADD_MEMBERSHIP,self.mreq)
except Exception, e:
dbg('WARNING: Failed to join multicast group:',e)
ok = False
示例11: bind_to_multicast
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [as 別名]
def bind_to_multicast(port=45454, multicast_group='239.255.43.21'):
import struct
# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Bind to the server address
# sock.bind((multicast_group, port))
sock.bind(('', port))
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
# group = socket.inet_aton(multicast_group)
# mreq = struct.pack('4sL', group, socket.INADDR_ANY)
mreq = struct.pack('4sl', socket.inet_aton(multicast_group), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
return sock
示例12: create_socket_ipv4_rx_mcast
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [as 別名]
def create_socket_ipv4_rx_mcast(self):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
except (IOError, OSError) as err:
self.warning("Could not create IPv4 UDP socket: %s", err)
return None
self.enable_addr_and_port_reuse(sock)
try:
sock.bind((self._multicast_address, self._local_port))
except (IOError, OSError) as err:
self.warning("Could not bind IPv4 UDP socket to address %s port %d: %s",
self._multicast_address, self._local_port, err)
return None
if sock is None:
return None
if self._local_ipv4_address:
req = struct.pack("=4s4s", socket.inet_aton(self._multicast_address),
socket.inet_aton(self._local_ipv4_address))
else:
req = struct.pack("=4sl", socket.inet_aton(self._multicast_address), socket.INADDR_ANY)
try:
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, req)
except (IOError, OSError) as err:
self.warning("Could not join IPv4 group %s for local address %s: %s",
self._multicast_address, self._local_ipv4_address, err)
return None
if not MACOS:
try:
# pylint:disable=no-member
sock.setsockopt(socket.IPPROTO_IP, socket.IP_PKTINFO, 1)
except (IOError, OSError) as err:
# Warn, but keep going; this socket option is not supported on macOS
self.warning("Could not set IP_PKTINFO socket option: %s", err)
try:
sock.setblocking(0)
except (IOError, OSError) as err:
self.warning("Could set multicast receive IPv4 UDP to non-blocking mode: %s", err)
return None
return sock
示例13: getfiles
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [as 別名]
def getfiles(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(3)
sock.bind((self.mcast_grp, self.mcast_port))
mreq = struct.pack("=4sl", socket.inet_aton(self.mcast_grp), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
loop = True
chunk = {}
chunk["end"] = 0
N = 335
#Wait for an end chunk to start by the beginning
while not (chunk["end"]):
chunk = self._getchunk(sock)
firstfile = str(chunk["filetype"])+"_"+str(chunk["fileid"])
#Loop until firstfile
while (loop):
xmldata=""
chunk = self._getchunk(sock)
#Discard headers
body=chunk["data"]
while not (chunk["end"]):
xmldata+=body
chunk = self._getchunk(sock)
body=chunk["data"]
#Discard last 4bytes binary footer?
xmldata+=body[:-4]
self._files[str(chunk["filetype"])+"_"+str(chunk["fileid"])]=xmldata
N = N - 1
if (str(chunk["filetype"])+"_"+str(chunk["fileid"]) == firstfile or N == 0):
loop = False
sock.close()
示例14: _multicast_struct
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [as 別名]
def _multicast_struct(self, address):
return struct.pack(
'4sl', socket.inet_aton(address), socket.INADDR_ANY)
示例15: init_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import INADDR_ANY [as 別名]
def init_socket(self):
ok = True
self.ip = '239.255.255.250'
self.port = 1900
try:
#This is needed to join a multicast group
self.mreq = struct.pack("4sl",socket.inet_aton(self.ip),socket.INADDR_ANY)
#Set up server socket
self.ssock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,socket.IPPROTO_UDP)
self.ssock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
try:
self.ssock.bind(('',self.port))
except Exception as e:
self.LogError("WARNING: Failed to bind %s:%d: %s" , (self.ip,self.port,e))
ok = False
try:
self.ssock.setsockopt(socket.IPPROTO_IP,socket.IP_ADD_MEMBERSHIP,self.mreq)
except Exception as e:
self.LogError('WARNING: Failed to join multicast group:'+str(e))
ok = False
except Exception as e:
self.LogError("Failed to initialize UPnP sockets:"+str(e))
return False
if ok:
self.LogDebug("Listening for UPnP broadcasts")
# ---------------- upnp_broadcast_responder.fileno -------------------------