Beispiel #1
0
class ImageSimpleCache(ImageCache):
    """Simple image cache."""
    def __init__(self, app=None):
        """Initialize the cache."""
        super(ImageSimpleCache, self).__init__(app=app)
        self.cache = SimpleCache()

    def get(self, key):
        """Return the key value.

        :param key: the object's key
        :return: the stored object
        :rtype: `BytesIO` object
        """
        return self.cache.get(key)

    def set(self, key, value, timeout=None):
        """Cache the object.

        :param key: the object's key
        :param value: the stored object
        :type value: `BytesIO` object
        :param timeout: the cache timeout in seconds
        """
        timeout = timeout or self.timeout
        self.cache.set(key, value, timeout)
        self.set_last_modification(key, timeout=timeout)

    def get_last_modification(self, key):
        """Get last modification of cached file.

        :param key: the file object's key
        """
        return self.get(self._last_modification_key_name(key))

    def set_last_modification(self, key, last_modification=None, timeout=None):
        """Set last modification of cached file.

        :param key: the file object's key
        :param last_modification: Last modification date of
            file represented by the key
        :type last_modification: datetime.datetime
        :param timeout: the cache timeout in seconds
        """
        if not last_modification:
            last_modification = datetime.utcnow().replace(microsecond=0)
        timeout = timeout or self.timeout
        self.cache.set(self._last_modification_key_name(key),
                       last_modification, timeout)

    def delete(self, key):
        """Delete the specific key."""
        self.cache.delete(key)
        self.cache.delete(self._last_modification_key_name(key))

    def flush(self):
        """Flush the cache."""
        self.cache.clear()
Beispiel #2
0
class UtilityTestCase(unittest.TestCase):
    def setUp(self):
        self.c = SimpleCache()

    def test_werkzeug_cache_get_or_add_missing_key(self):
        self.assertEquals('bar',
                          werkzeug_cache_get_or_add(self.c, 'foo', 'bar', 10))

    def test_werkzeug_cache_get_or_add_existing_key(self):
        self.c.set('foo', 'bar')
        self.assertEquals('bar',
                          werkzeug_cache_get_or_add(self.c, 'foo', 'qux', 10))
Beispiel #3
0
class OpaqueValidator:
    def __init__(self,
                 introspection_url,
                 client_id,
                 client_secret,
                 verify_ssl_server=True):
        self.ctx = ssl.create_default_context()

        if not verify_ssl_server:
            self.ctx.check_hostname = False
            self.ctx.verify_mode = ssl.CERT_NONE

        self._introspection_url = introspection_url
        self._client_id = client_id
        self._client_secret = client_secret

        self._token_cache = SimpleCache()
        self.logger = logging.getLogger(__name__)

    def introspect_token(self, token):

        params = {'token': token}

        headers = {
            'content-type':
            'application/x-www-form-urlencoded',
            'accept':
            'application/jwt, application/json;q=0.9, text/plain;q=0.8, text/html;q=0.7'
        }

        req = request("POST",
                      self._introspection_url,
                      allow_redirects=False,
                      auth=(self._client_id, self._client_secret),
                      verify=self.ctx.check_hostname,
                      data=params,
                      headers=headers)

        response_content_type = req.headers.get("Content-Type",
                                                "text/plain").split(";")[0]
        result = {}
        cache_duration_header_value = req.headers.get("Cache-Duration", None)

        if cache_duration_header_value:
            # Turn 'public, max-age=31536000' into {'public': None, 'max-age': '31536000'}
            cache_duration_parts = dict(
                (part_values[0],
                 None if len(part_values) == 1 else part_values[1])
                for part_values in [
                    part.strip().split("=")
                    for part in cache_duration_header_value.split(",")
                ])

            if "public" in cache_duration_parts:
                result["cache_timeout"] = int(cache_duration_parts["max-age"])

        if req.status_code == 200:
            if response_content_type == "application/json":
                result.update(json.loads(req.text))
            elif response_content_type == "application/jwt":
                jws = jwkest.jws.factory(req.text)

                if jws is not None and len(jws.jwt.part) >= 2:
                    result["active"] = True
                    result.update(json.loads(jws.jwt.part[1]))
            else:
                self.logger.warning(
                    "Response type from introspection endpoint was unsupported, response_type = "
                    + response_content_type)

                raise Exception(
                    "Response type is from introspect endpoint is " +
                    response_content_type, req.text)
        elif req.status_code == 204:
            result.update(dict(active=False))
        else:
            raise Exception("HTTP POST error from introspection: %s" %
                            req.status_code)

        return result

    def validate(self, token):

        now = calendar.timegm(datetime.utcnow().utctimetuple())

        # Lookup in cache:
        cached_response = self._token_cache.get(token)
        if cached_response is not None:
            if cached_response['active']:
                if cached_response['exp'] >= now:
                    return cached_response
            else:
                return dict(active=False)

        introspect_response = self.introspect_token(token)
        cache_timeout = 0

        if "cache_timeout" in introspect_response:
            cache_timeout = introspect_response["cache_timeout"]
        elif "exp" in introspect_response:
            cache_timeout = introspect_response["exp"] - now

        if "active" not in introspect_response:
            # The token isn't know to be active, so we'll never introspect it again
            introspect_response["active"] = False

            self._token_cache.set(token,
                                  introspect_response,
                                  timeout=cache_timeout)

            raise OpaqueValidatorException(
                "No active field in introspection response")

        self._token_cache.set(token,
                              introspect_response,
                              timeout=cache_timeout)

        if not introspect_response['active']:
            return {"active": False}

        if 'sub' not in introspect_response:
            raise OpaqueValidatorException(
                "Missing sub field in introspection response")

        if 'exp' not in introspect_response:
            raise OpaqueValidatorException(
                "Missing exp field in introspection response")

        if 'scope' not in introspect_response:
            raise OpaqueValidatorException(
                "Missing scope field in introspection response")

        return introspect_response
Beispiel #4
0
    return success_handle(output)


#def app(environ, start_response):
#    """Simplest possible application object"""
#    data = b'Hello, World!\n'
#    status = '200 OK'
#    response_headers = [
#        ('Content-type', 'text/plain'),
#        ('Content-Length', str(len(data)))
#    ]
#    start_response(status, response_headers)
#    return iter([data])

if __name__ == "__main__":
    cache.set('global_obj_count', 0)
    cache.set('global_value_time', 0)
    # lock to control access to variable
    dataLock = threading.Lock()

    def test_job():
        while True:
            print("Run recurring task")
            time.sleep(3)

    def people_counter_job(camera):
        net2 = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
        obj_count = 0
        while True:
            time.sleep(5.0)
            label = '{:%d.%m.%Y %H:%M:%S}'.format(datetime.datetime.now())
Beispiel #5
0
    # return json.dumps({"api": info})
    return json.dumps(info, default=customconverter)


def success_handle(output, status=200, mimetype='application/json'):
    return Response(output, status=status, mimetype=mimetype)


@app.route('/api', methods=['GET'])
def homepage():
    output = json.dumps({"api": '1.0'})
    return success_handle(output)


if __name__ == "__main__":
    cache.set('global_obj_count', 0)

    def test_job():
        while True:
            print("Run recurring task")
            time.sleep(3)

    def people_counter_job(camera):
        while True:
            frame = camera.get_frame()
            time.sleep(1.0)
            frame = imutils.resize(frame, width=400)
            (h, w) = frame.shape[:2]
            blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
                                         0.007843, (300, 300), 127.5)