"belltown" wrote:
Here's a little Python 3 program that you can run on your Pi to respond to M-SEARCH requests from a Roku. As it's written right now, it impersonates a Roku device, but you can change the "ST" field to something else, and make sure the Roku code searches for the same value.
"squirreltown" wrote:
I ... added a function to insert the local ip address into the MSEARCH response
"belltown" wrote:"squirreltown" wrote:
I ... added a function to insert the local ip address into the MSEARCH response
The code I posted already does that. Unless you mean you re-wrote that part as a function to make it clearer, which is always a good idea.
import socket
import struct
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 1900))
addMembershipData = struct.pack('4sl', socket.inet_aton('239.255.255.250'), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, addMembershipData)
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
return s.getsockname()[0]
MSEARCH_RESPONSE = ('HTTP/1.1 200 OK\r\n'
'Cache-Control: max-age=3600\r\n'
'ST: raspberr\r\r\n'
'Location: http://'+get_ip_address()+':8060/\r\n'
'\r\n')
while True:
data, addr = sock.recvfrom(4096)
if data.startswith(b'M-SEARCH'):
print data
response = str.encode(MSEARCH_RESPONSE.format(ipAddr=addr[0]))
sock.sendto(response, addr)
response = str.encode(MSEARCH_RESPONSE.format(ipAddr=addr[0]))
response = str.encode(MSEARCH_RESPONSE.format(ipAddr=get_ip_address())
response = str.encode(MSEARCH_RESPONSE)
"belltown" wrote:
You're right, you do need to get your local address and pass that into the M-SEARCH string. In my example, I was passing in the address obtained from recvfrom(), which only works if the client and server are running on the same machine, as they were when I "tested" it.
"squirreltown" wrote:
EnTerr - Here I am trying to be less ignorant and you're complaining. 🙂
"EnTerr" wrote:"belltown" wrote:
You're right, you do need to get your local address and pass that into the M-SEARCH string. In my example, I was passing in the address obtained from recvfrom(), which only works if the client and server are running on the same machine, as they were when I "tested" it.
Should just do `sock.getsockname()` on the socket at hand instead of the contraption to connect google DNS. Not only faster/simpler but also more "righteous" - imagine a multihomed (>1 IP) host, where Roku is on a different interface than the internet.
"EnTerr" wrote:
the more complicated (more elements, more interactions, more points of failure) a system is, the more fragile it is.