예제 #1
0
파일: webcam.py 프로젝트: dairiki/puppyserv
    def _open_stream(self):
        self.conn.request("GET", self.path, headers=self.request_headers)
        resp = self.conn.getresponse()
        content_type = None
        try:
            if resp.status != 200 or resp.msg.getmaintype() != 'multipart':
                raise ConnectionError(
                    u"Unexpected response: {resp.status}\n"
                    u"{resp.msg}\n{data}"
                    .format(data=resp.read(), **locals()))
            log.debug("Opened stream\n%s", resp.msg)
            boundary = resp.msg.getparam('boundary')
            assert boundary

            fp = ReadlineAdapter(resp)
            while True:
                sep = fp.readline().rstrip()
                if not sep:
                    # XXX: instead of this should just read two bytes
                    # after the end of the data?
                    sep = fp.readline().rstrip()
                if sep != b'--' + boundary:
                    if sep != b'--' + boundary + b'--':
                        raise StreamingError(u"Bad boundary %r" % sep)
                    break
                msg = Message(fp, seekable=0)
                content_length = int(msg['content-length'])
                # XXX: impose maximum limit on content_length?
                data = fp.read(content_length)
                if content_type:
                    bad_type = msg.gettype() != content_type
                else:
                    bad_type = msg.getmaintype() != 'image'
                    content_type = msg.gettype()
                if bad_type:
                    raise StreamingError(
                        u"Unexpected content-type\n{msg}\n{data}"
                        .format(**locals()))
                log.debug("Got part\n%s", msg)
                yield VideoFrame(data, msg.gettype())

        finally:
            resp.close()
예제 #2
0
파일: webcam.py 프로젝트: dairiki/puppyserv
    def _open_stream(self):
        self.conn.request("GET", self.path, headers=self.request_headers)
        resp = self.conn.getresponse()
        content_type = None
        try:
            if resp.status != 200 or resp.msg.getmaintype() != 'multipart':
                raise ConnectionError(u"Unexpected response: {resp.status}\n"
                                      u"{resp.msg}\n{data}".format(
                                          data=resp.read(), **locals()))
            log.debug("Opened stream\n%s", resp.msg)
            boundary = resp.msg.getparam('boundary')
            assert boundary

            fp = ReadlineAdapter(resp)
            while True:
                sep = fp.readline().rstrip()
                if not sep:
                    # XXX: instead of this should just read two bytes
                    # after the end of the data?
                    sep = fp.readline().rstrip()
                if sep != b'--' + boundary:
                    if sep != b'--' + boundary + b'--':
                        raise StreamingError(u"Bad boundary %r" % sep)
                    break
                msg = Message(fp, seekable=0)
                content_length = int(msg['content-length'])
                # XXX: impose maximum limit on content_length?
                data = fp.read(content_length)
                if content_type:
                    bad_type = msg.gettype() != content_type
                else:
                    bad_type = msg.getmaintype() != 'image'
                    content_type = msg.gettype()
                if bad_type:
                    raise StreamingError(
                        u"Unexpected content-type\n{msg}\n{data}".format(
                            **locals()))
                log.debug("Got part\n%s", msg)
                yield VideoFrame(data, msg.gettype())

        finally:
            resp.close()