GAE/Python请求转发?
我想知道是否有一个相当于我在 Java servlet 编程中习惯的 RequestDispatcher.forward(request, response) 的 Pythonic 等价物?这是 Java 中一种很常见的技术,它使您能够对特定类型的请求进行一些预处理,然后移交给另一个 url 处理程序。这一切都发生在服务器内部,这可以节省大量时间。
我在 GAE/Python 文档中看到的最接近的东西是 RequestHandler.redirect(),但这是没有希望的。一方面,有一个额外的浏览器往返行程。另一方面,一旦重定向脱离我的控制,就不能保证它会被真正遵循,这让我有点紧张。 (从语义上讲,这也是错误的,因为重定向意味着原始资源可能不可用或已移动,但事实并非如此。)
webapp2 中有一些听起来非常接近的东西,描述于 http://webapp-improved.appspot.com/api/webapp2.html#webapp2.RedirectHandler;但从我从相当粗略的文档中可以猜测,它只是为了进行某种 url 重写,这根本不是我想要的。
I was wondering if there's a pythonic equivalent of the RequestDispatcher.forward(request, response) that I'm used to from Java servlet programming? It's a common enough technique in Java, and enables you to do, say, a little preprocessing of a particular type of request, and then hand over to another url handler. This all happens inside the server, which is an enormous time saver.
The nearest thing I can see in the GAE/Python documentation is RequestHandler.redirect(), but that's hopeless. For one thing, there's an extra round trip to the browser. For another, there's no guarantee the redirect will actually be followed once it's out of my hands, which makes me a little twitchy. (Semantically it's just wrong too, since a redirect implies that the original resource may be unavailable or have moved, which ain't the case.)
There's something that sounds tantalisingly close in webapp2, described at http://webapp-improved.appspot.com/api/webapp2.html#webapp2.RedirectHandler; but from what I can guess from the rather sketchy documentation, it's just for doing a kind of url-rewriting, which is not at all what I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道 GAE,但在 django 中你可以做这样的事情:
在 GAE 中也许你可以这样做:(
尽管这取决于功能,可能会把事情搞砸,例如,如果
View.操作
使用self.something_which_should_be_here_but_isnt_because_self_is_some_other_thing
)I don't know about GAE, but in django you can do something like this:
In GAE maybe you can do this:
(though this, depending on the function, might screw things up, if, for example,
View.action
usesself.something_which_should_be_here_but_isnt_because_self_is_some_other_thing
)最简单的方法就是将相关代码重构为从两个处理程序调用的函数,或者将功能放入两个处理程序扩展的基类中。哪个更合适取决于您的应用程序。
The easiest way to do this is simply to refactor the relevant code into a function that you call from both handlers, or to put the functionality in a base class both handlers extend. Which is more suitable depends on your app.