Google App Engine 中的请求感知代码 - os.environ?

发布于 2024-08-21 15:03:09 字数 551 浏览 1 评论 0原文

在 GAE 中,您可以说 users.get_current_user() 获取当前请求隐含的当前登录用户。即使同时处理多个请求,这种方法也能工作——users 模块以某种方式知道 get_current_user 函数正在代表哪个请求被调用。我查看了开发服务器中模块的代码,它似乎使用 os.environ 来获取用户电子邮件和与当前请求关联的其他值。

这是否意味着每个请求都会获得一个独立的 os.environ 对象?

我需要实现一个类似于 users.get_current_user() 的服务,该服务将根据调用代码处理的请求返回不同的值。假设 os.environ 是可行的方法,我如何知道哪些变量名称已被 GAE 使用(或保留)?

另外,有没有办法添加一个在每个请求之前调用的钩子(或事件处理程序)?

In GAE, you can say users.get_current_user() to get the currently logged-in user implicit to the current request. This works even if multiple requests are being processed simultaneously -- the users module is somehow aware of which request the get_current_user function is being called on behalf of. I took a look into the code of the module in the development server, and it seems to be using os.environ to get the user email and other values associated to the current request.

Does this mean that every request gets an independent os.environ object?

I need to implement a service similar to users.get_current_user() that would return different values depending on the request being handled by the calling code. Assuming os.environ is the way to go, how do I know which variable names are already being used (or reserved) by GAE?

Also, is there a way to add a hook (or event handler) that gets called before every request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2024-08-28 15:03:09

正如文档所说,

Python Web 应用程序与
使用 CGI 的 App Engine Web 服务器
协议。

这基本上意味着在任何给定进程中一次只服务一个请求(尽管与真正的 CGI 不同,一个进程可以连续地重用多个请求,一个接一个,如果它定义了 main app.yaml 分派到的各个模块中的函数)。另请参阅此页面这个 用于 CGI 定义和使用的环境变量的文档。

App Engine 定义的挂钩是围绕 RPC 层的调用,而不是 HTTP 请求。要在提供服务之前拦截每个请求,您可以使用 app.yaml 将所有请求重定向到单个 .py 文件,并在该文件的 main 中执行拦截重定向之前的 函数(或者,您可以在使用 app.yaml 分派到的每个模块中的 main 开头调用挂钩)。

As the docs say,

A Python web app interacts with the
App Engine web server using the CGI
protocol.

This basically means exactly one request is being served at one time within any given process (although, differently from real CGI, one process can be serially reused for multiple requests, one after the other, if it defines main functions in the various modules to which app.yaml dispatches). See also this page, and this one for documentation of the environment variables CGI defines and uses.

The hooks App Engine defines are around calls at the RPC layer, not the HTTP requests. To intercept each request before it gets served, you could use app.yaml to redirect all requests to a single .py file and perform your interception in that file's main function before redirecting (or, you could call your hook at the start of the main in every module you're using app.yaml to dispatch to).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文