Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to SSDPClient include client IP in m_search responses. #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions ssdpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self, proto="ipv4", port=1900, ttl=2, iface=None, timeout=5, addres
if proto not in allowed_protos:
raise ValueError("Invalid proto - expected one of {}".format(allowed_protos))
self.port = port
self.include_addr = kwargs.get('include_addr', False)
if proto == "ipv4":
af_type = socket.AF_INET
self.broadcast_ip = ipv4_multicast_ip
Expand All @@ -34,6 +35,8 @@ def __init__(self, proto="ipv4", port=1900, ttl=2, iface=None, timeout=5, addres
if address is not None:
self.sock.bind((address, 0))
if iface is not None:
if isinstance(iface, str):
iface = bytes(iface, encoding='iso-8859-15')
self.sock.setsockopt(socket.SOL_SOCKET, SO_BINDTODEVICE, iface)
if proto == "ipv6":
# Specifically set multicast on interface
Expand All @@ -46,8 +49,7 @@ def send(self, data):
def recv(self):
try:
while True:
data = self.sock.recv(1024)
yield data
yield self.sock.recvfrom(1024)
except socket.timeout:
pass
return
Expand All @@ -67,11 +69,14 @@ def m_search(self, st="ssdp:all", mx=1):
host = "{}:{}".format(self.broadcast_ip, self.port)
data = create_msearch_payload(host, st, mx)
self.send(data)
responses = [x for x in self.recv()]
responses = list(self.recv())
parsed_responses = []
for response in responses:
for response, address in responses:
try:
headers = parse_headers(response)
if self.include_addr:
addr, port = address
headers['SRC-ADDR'] = f'{addr}:{port}'
parsed_responses.append(headers)
except ValueError:
# Invalid response, do nothing.
Expand Down
2 changes: 1 addition & 1 deletion ssdpy/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


# Python 2 doesn't have socket.if_nametoindex so we need to implement it manually
if LINUX:
if LINUX or MACOSX:
if PY2:
import ctypes.util

Expand Down