|
|
@@ -7,10 +7,12 @@ |
|
|
|
# Implementation of SSDP server under Twisted Python. |
|
|
|
# |
|
|
|
|
|
|
|
import random |
|
|
|
import string |
|
|
|
|
|
|
|
from twisted.python import log |
|
|
|
from twisted.internet.protocol import DatagramProtocol |
|
|
|
from twisted.internet import reactor |
|
|
|
|
|
|
|
# TODO: Is there a better way of hooking the SSDPServer into a reactor |
|
|
|
# without having to know the default SSDP port and multicast address? |
|
|
@@ -45,7 +47,7 @@ class SSDPServer(DatagramProtocol): |
|
|
|
lines = map(lambda x: x.replace(': ', ':', 1), lines[1:]) |
|
|
|
lines = filter(lambda x: len(x) > 0, lines) |
|
|
|
|
|
|
|
headers = [string.split(x, ':', 1) for x in lines[1:]] |
|
|
|
headers = [string.split(x, ':', 1) for x in lines] |
|
|
|
headers = dict(map(lambda x: (x[0].lower(), x[1]), headers)) |
|
|
|
|
|
|
|
if cmd[0] == 'M-SEARCH' and cmd[1] == '*': |
|
|
@@ -82,11 +84,12 @@ class SSDPServer(DatagramProtocol): |
|
|
|
for k, v in self.known[headers['st']].items(): |
|
|
|
response.append('%s: %s' % (k, v)) |
|
|
|
|
|
|
|
log.msg('responding with: %s' % response) |
|
|
|
delay = random.randint(0, int(headers['mx'])) |
|
|
|
log.msg('responding in %d with: %s' % (delay, response)) |
|
|
|
|
|
|
|
# TODO: we should wait random(headers['mx']) |
|
|
|
self.transport.write( |
|
|
|
string.join(response, '\r\n') + '\r\n\r\n', (host, port)) |
|
|
|
reactor.callLater(delay, self.transport.write, |
|
|
|
string.join(response, '\r\n') + '\r\n\r\n', (host, port)) |
|
|
|
|
|
|
|
def register(self, usn, st, location): |
|
|
|
"""Register a service or device that this SSDP server will |
|
|
|