How to Respond to SSDP Searches with Sockets in Python? -
i'm trying create chromecast device can stream video internet , remotely controlled. remote control http requests device , listen them following code:
listening http requests device (localhost):
import socket import sys s = socket.socket() host = "localhost" port = 8060 s.bind(('', port)) s.listen(1) try: while true: connection, clientaddress = s.accept() try: print clientaddress //do command //reply except: print "error" except keyboardinterrupt: print('interrupt')
i went implement ssdp other devices can find device , cast , planning on using similar code listen msearch requests except on 239.255.255.250:1900. however, when msearch sent code not pick up.
listening ssdp searches on "239.255.255.250:1900"
import socket import sys s = socket.socket() host = "239.255.255.250" port = 1900 s.bind((host, port)) s.listen(10) try: while true: connection, clientaddress = s.accept() try: print("trigger") print clientaddress data = connection.recv(1048) print data except: print "error" except keyboardinterrupt: print('interrupt')
the question:
so question why 2 acting differently (i believe because in first example device listening destination http requests whereas in second not) , way fix code can listen ssdp searches.
ssdp udp protocol, don't specify udp in socket constructor. using socket constructor no arguments,
s = socket.socket()
you default arguments:
socket.socket(family=af_inet, type=sock_stream, proto=0, fileno=none)
as can see, default type sock_stream
, i.e. tcp. instead, @ server() method in this example, of fragment is:
mcast_grp = '239.255.255.250' mcast_port = 1900 def server(timeout=5): socket.setdefaulttimeout(timeout) 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.bind(('', mcast_port)) mreq = struct.pack('4sl', socket.inet_aton(mcast_grp), socket.inaddr_any) sock.setsockopt(socket.ipproto_ip, socket.ip_add_membership, mreq) # ... more code @ link
wiki
Comments
Post a Comment