Exemple #1
0
    class SSDPResponse(object):
        """A container class for received SSDP responses."""

        def __init__(self, response):
            """Initializes the SSDPResponse instance based on a response string"""
            super(BasicSSDPServiceDiscoverer.SSDPResponse, self).__init__()

            # Initialize fields
            self.Location = None
            self.USN = None
            self.ST = None
            self.Headers  = CaseInsensitiveDict()

            # Parse response
            self._fromString(response)

        def _fromString(self, str):
            """Parses a response string and assigns values to the SSDPResponse object.

            :param str str: The string to parse."""

            # Lazy method to parse all http-headers
            h = CaseInsensitiveDict({k.lower(): v for k, v in dict(re.findall(r'(?P<name>.*?): (?P<value>.*?)\r\n', str)).items()})
            self.Headers = h

            # Set major fields
            if 'location' in h:
                self.Location = h['location']

            if 'USN' in h:
                self.USN = h['USN']

            if 'ST' in h:
                self.ST = h['ST']

        def __repr__(self):
            return '<SSDPResponse from %s at %s; Headers: %s>' % (self.USN, self.Location, self.Headers.__repr__())

        def __hash__(self):
            if self.USN is not None:
                return hash(self.USN)

            return hash(tuple(self.Headers.items()))

        def __eq__(self, other):
            if self is not None and other is None:
                return False

            if not isinstance(other, self.__class__):
                return False

            return hash(self) == hash(other)

        def __ne__(self, other):
            return not self.__eq__(other)