本文整理汇总了Python中socket.IP_MULTICAST_TTL属性的典型用法代码示例。如果您正苦于以下问题:Python socket.IP_MULTICAST_TTL属性的具体用法?Python socket.IP_MULTICAST_TTL怎么用?Python socket.IP_MULTICAST_TTL使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.IP_MULTICAST_TTL属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [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()')
示例2: _init_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [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
示例3: checkState
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def checkState(self):
"""Detects and publishes any state change"""
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, 2)
sock.sendto(self.ssdpRequest, ("239.255.255.250", 1900))
while True:
try:
resp = sock.recv(1024)
matchObj = re.match(r'.*USN: uuid:roku:ecp:([\w\d]{12}).*LOCATION: (http://.*/).*', resp, re.S)
name = matchObj.group(1)
ip = matchObj.group(2)
if name not in sorted(self.ips.keys()) or self.ips[name] != ip:
self.logger.info('%s is now at %s' % (name, ip))
self.ips[name] = ip
self.publish(self.ips[name], self.rokus[name])
else :
self.logger.info('%s is still at %s' % (name, ip))
except socket.timeout:
break
sock.close()
示例4: init_v4
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def init_v4(self):
idx = socket.if_nametoindex(self.interface.name)
self.multicast_address = (WSD_MCAST_GRP_V4, WSD_UDP_PORT)
# v4: member_request (ip_mreqn) = { multicast_addr, intf_addr, idx }
mreq = (
socket.inet_pton(self.family, WSD_MCAST_GRP_V4) +
socket.inet_pton(self.family, self.address) +
struct.pack('@I', idx))
self.recv_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
try:
self.recv_socket.bind((WSD_MCAST_GRP_V4, WSD_UDP_PORT))
except OSError:
self.recv_socket.bind(('', WSD_UDP_PORT))
self.send_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_IF, mreq)
self.send_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)
self.send_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, args.hoplimit)
self.listen_address = (self.address, WSD_HTTP_PORT)
示例5: sender
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def sender(group):
addrinfo = socket.getaddrinfo(group, None)[0]
s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)
# Set Time-to-live (optional)
ttl_bin = struct.pack('@i', MYTTL)
if addrinfo[0] == socket.AF_INET: # IPv4
s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin)
else:
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin)
while True:
data = repr(time.time()).encode('utf-8') + b'\0'
s.sendto(data, (addrinfo[4][0], MYPORT))
time.sleep(1)
示例6: discover
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def discover(service, timeout=5, retries=1, mx=3):
group = (b"239.255.255.250", 1900)
message = "\r\n".join([
'M-SEARCH * HTTP/1.1',
'HOST: {0}:{1}',
'MAN: "ssdp:discover"',
'ST: {st}',
'MX: {mx}',
'',
''])
socket.setdefaulttimeout(timeout)
responses = []
for _ in range(retries):
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, 2)
sock.sendto(message.format(*group, st=service, mx=mx).encode('utf-8'), group)
while True:
try:
resp = SSDPResponse(sock.recv(1024))
# responses[resp.location] = resp
responses.append(resp)
except socket.timeout:
break
return responses
示例7: perform_ssdp_discovery
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def perform_ssdp_discovery(service, timeout=5, retries=5, mx=3):
# Standard multicast IP address and port for SSDP.
group = ("239.255.255.250", 1900)
message = "\r\n".join([
"M-SEARCH * HTTP/1.1",
"HOST: {0}:{1}",
'MAN: "ssdp:discover"',
"ST: {st}",
"MX: {mx}"
]).format(*group, st=service, mx=mx)
socket.setdefaulttimeout(timeout)
sources = set()
for _ in range(retries):
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, 2)
sock.sendto(message.encode('utf-8'), group)
while True:
try:
data, addr = sock.recvfrom(1024)
response = SsdpResponse(data, addr)
if addr[0] not in sources:
# We only want to return each node once.
sources.add(addr[0])
yield response
except socket.timeout:
break
示例8: _server_discovery
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def _server_discovery(self):
MULTI_GROUP = ("<broadcast>", 7359)
MESSAGE = "who is EmbyServer?"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(1.0) # This controls the socket.timeout exception
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.SO_REUSEADDR, 1)
LOG.debug("MultiGroup : %s", str(MULTI_GROUP))
LOG.debug("Sending UDP Data: %s", MESSAGE)
servers = []
try:
sock.sendto(MESSAGE, MULTI_GROUP)
except Exception as error:
LOG.error(error)
return servers
while True:
try:
data, addr = sock.recvfrom(1024) # buffer size
servers.append(json.loads(data))
except socket.timeout:
LOG.info("Found Servers: %s", servers)
return servers
except Exception as e:
LOG.error("Error trying to find servers: %s", e)
return servers
示例9: _search
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def _search(self, host, ssdp_ttl, ssdp_mx, ssdp_amount):
logger.debug('Binding socket to "{}" ...'.format(host or ''))
sock = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.settimeout(ssdp_mx)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(
socket.IPPROTO_IP,
socket.IP_MULTICAST_TTL,
ssdp_ttl)
sock.bind((host, self.MSEARCH_PORT))
for i in range(1, ssdp_amount + 1):
t = threading.Timer(
float(i) / 2, self._send_discover, args=[sock, ssdp_mx])
t.start()
while True:
try:
header, address = sock.recvfrom(self.BUFFER_SIZE)
if self.cb_on_device_response:
guess = chardet.detect(header)
header = header.decode(guess['encoding'])
header = pulseaudio_dlna.plugins.dlna.ssdp._get_header_map(
header)
self.cb_on_device_response(header, address)
except socket.timeout:
break
sock.close()
示例10: receiver
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def receiver(service='mihome'):
from plugins import gateway
assert service in MULTICAST, 'No such service'
store = get_store()
address, port = MULTICAST.get(service)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", port))
mreq = struct.pack("=4sl", socket.inet_aton(address), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, SOCKET_BUFSIZE)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
sock.settimeout(20) # 2x of heartbeat period
current = {}
while True:
try:
data, _ = sock.recvfrom(SOCKET_BUFSIZE) # buffer size is 1024 bytes
except socket.timeout:
continue
print(datetime.now().isoformat(), data)
if service == 'mihome':
message = json.loads(data.decode())
data = json.loads(message['data'])
if message.get('model') in ('sensor_ht', 'weather.v1') and not sensor_ht.process(conn, cursor, current, message, data):
continue
elif message.get('model') == 'magnet':
magnet.process(store, message, data)
elif message.get('model') == 'gateway':
gateway.process(store, message, data)
current = {}
elif service == 'yeelight':
yeelight.process(data.decode())
示例11: discover
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def discover():
from mihome import MULTICAST, SOCKET_BUFSIZE
# Empty stored list
store.delete(STORE_KEY)
address, port = MULTICAST.get('yeelight')
yee_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
yee_socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
yee_socket.sendto('\r\n'.join(YEE_DISCOVER).encode(), (address, port))
while True:
data, _ = yee_socket.recvfrom(SOCKET_BUFSIZE)
add_device(get_data(data.decode()))
示例12: send
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def send(data, *, group=DEFAULT_GROUP, port=DEFAULT_PORT, ttl=None):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
if ttl is not None:
packed_ttl = struct.pack('b', ttl)
s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, packed_ttl)
try:
s.sendto(data, (group, port))
finally:
s.close()
示例13: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def __init__(self, summary_table, group=DEFAULT_GROUP, port=DEFAULT_PORT, ttl=None):
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
try:
if ttl is not None:
packed_ttl = struct.pack('b', ttl)
self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, packed_ttl)
except Exception:
self._socket.close()
raise
self._summary_table = summary_table
self._group = group
self._port = port
示例14: discover_MediaPlayer
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def discover_MediaPlayer(self):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 4)
s.bind(("", SSDP_BROADCAST_PORT + 20))
s.sendto(SSDP_BROADCAST_MSG.encode("UTF-8"), (SSDP_BROADCAST_ADDR,
SSDP_BROADCAST_PORT))
s.settimeout(5.0)
devices = []
while True:
try:
data, addr = s.recvfrom(self.SOCKET_BUFSIZE)
if len(data) is None:
continue
except socket.timeout:
break
try:
info = [a.split(":", 1) for a in data.decode("UTF-8").split("\r\n")[1:]]
device = dict([(a[0].strip().lower(), a[1].strip()) for a in info if len(a) >= 2])
devices.append(device)
except:
pass
devices_urls = [device["location"] for device in devices if "AVTransport" in device["st"]]
devices_urls = list(set(devices_urls))
devices = []
for location_url in devices_urls:
device = self.register_device(location_url)
if device != None:
devices.append(device)
return devices
示例15: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_MULTICAST_TTL [as 别名]
def __init__(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.settimeout(3.0)
self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(('', 0))