def reset_environment_middleware(app, frozen_environment, frozen_user_env,
                                 frozen_env_config_env):

    @wrappers.Request.application
    def reset_environment_wrapper(request):
        """Reset the system environment and populate it with wsgi_env."""
        # Wipe os.environ entirely.
        os.environ.clear()

        # Populate os.environ in order, so that later steps overwrite
        # conflicting keys in previous steps.

        # Add in user env variables specified in app.yaml. These are added
        # first because they should not take precedence over any conflicting
        # key.
        os.environ.update(frozen_user_env)

        # Repopulate os.environ with the frozen environment.
        os.environ.update(frozen_environment)

        # Patch the wsgi environment to set REMOTE_ADDR to equal the origin IP
        # and not the IP of the proxy server. This mimics existing GAE
        # behavior.
        mutate_env_to_overwrite_remote_addr(request.environ)

        # Add in wsgi_env data, including request headers.
        os.environ.update(request_environment_for_wsgi_env(request.environ))

        # Add in configuration data from env_config.
        os.environ.update(frozen_env_config_env)

        # Add reserved keys, which draw from wsgi_env as well. These have a
        # very high priority and so are added nearly last.
        os.environ.update(reserved_env_keys_for_wsgi_env(request.environ))

        # Tweak the environment to hide the service bridge.
        os.environ.update(get_env_to_hide_service_bridge(request.environ))

        return app

    return reset_environment_wrapper
Replace the contents of os.environ with a frozen env + request data.
dataset/ETHPy150Open GoogleCloudPlatform/python-compat-runtime/appengine-vmruntime/vmruntime/middleware.py/reset_environment_middleware