From 82eba4cbccf1b86ba19108d025d8ac26d5b944f6 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Thu, 9 Feb 2006 00:38:04 -0800 Subject: [PATCH] don't immediately reply to the request, wait a random time [0, MX] before repling as per draft-goland-http-udp-04.txt [git-p4: depot-paths = "//depot/": change = 720] --- SSDP.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/SSDP.py b/SSDP.py index 9ead2d8..6f117a1 100644 --- a/SSDP.py +++ b/SSDP.py @@ -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