如何为充当哑代理的 Web 框架编写路由/视图/控制器?

发布于 2024-08-22 20:13:55 字数 376 浏览 3 评论 0原文

也就是说,假设我正在编写托管在 foo.com 上的内容。我希望访问 foo.com/bar.com 的用户能够从 foo.com< 获得 bar.com 服务/code> 并能够与 bar.com 交互(例如通过 point- 导航到 foo.com/bar.com/baz并单击)。我知道这就是代理应该做的事情。我需要对访问代理的请求进行一些预处理,这就是我转向 Web 框架的原因。我更喜欢 django、rails、sinatra 或其他 python/ruby 解决方案,但实际上任何一个都可以。

提前致谢;欢迎提出其他建议。

That is to say, let's say I'm writing something that's hosted on foo.com. I'd like it to be possible for a user who goes to foo.com/bar.com to be served up bar.com from foo.com and to be able to interact with bar.com (e.g. navigate to foo.com/bar.com/baz via point-and-click). I understand that this is what a proxy is supposed to do. I need to do some preprocessing of a request to access the proxy, which is why I'm turning to a web framework. I've a preference for django, rails, or sinatra, or another python/ruby solution, but any will do, really.

Thanks in advance; alternate suggestions are welcome.

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

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

发布评论

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

评论(2

萤火眠眠 2024-08-29 20:13:56

首先,您需要解析 foo.com 上的 URL。在 django 中,你可以有一个像这样的 url(未测试):

url(r'(?P<url>.*)

所以 http://foo.com/ bar.com/baz/ 将为您提供“bar.com/baz/”的网址,您可以在您的视图中随意使用。

然后您必须使用像 urllib2 这样的库检索 bar.com 上的页面。

当您拥有远程页面的内容时,您需要更改所有指向 bar.com 的链接(锚元素)以指向代理的 URL。如果您想代理图像、样式表和 JavaScript,您还需要更改它们的链接。

您可能还希望尽可能多地缓存。并且一定要在 urllib 请求上设置一个用户代理,让其他站点知道这是某种机器人或代理。


话虽如此,这听起来确实是一个愚蠢的想法。您的用例是什么?

, my_proxy_view, name = 'proxy')

所以 http://foo.com/ bar.com/baz/ 将为您提供“bar.com/baz/”的网址,您可以在您的视图中随意使用。

然后您必须使用像 urllib2 这样的库检索 bar.com 上的页面。

当您拥有远程页面的内容时,您需要更改所有指向 bar.com 的链接(锚元素)以指向代理的 URL。如果您想代理图像、样式表和 JavaScript,您还需要更改它们的链接。

您可能还希望尽可能多地缓存。并且一定要在 urllib 请求上设置一个用户代理,让其他站点知道这是某种机器人或代理。


话虽如此,这听起来确实是一个愚蠢的想法。您的用例是什么?

First you will need to parse the URL at foo.com. In django you could have an url like this(not tested):

url(r'(?P<url>.*)

So http://foo.com/bar.com/baz/ will give you an url of 'bar.com/baz/' you may use as you please in your view.

Then you have to retrieve the page at bar.com, using a library like urllib2.

When you have the contents of the remote page, you need to change all links(anchor elements) that point to bar.com to point to the URLs of your proxy. If you want to proxy images, stylesheets and javascript you need to change the links of those as well.

You probably want to cache as much as possible as well. And be sure to set a user-agent on the urllib request that will let the other site know that this is some kind of robot or proxy.


With that said, this sounds like a really stupid idea. What is your use case?

, my_proxy_view, name = 'proxy')

So http://foo.com/bar.com/baz/ will give you an url of 'bar.com/baz/' you may use as you please in your view.

Then you have to retrieve the page at bar.com, using a library like urllib2.

When you have the contents of the remote page, you need to change all links(anchor elements) that point to bar.com to point to the URLs of your proxy. If you want to proxy images, stylesheets and javascript you need to change the links of those as well.

You probably want to cache as much as possible as well. And be sure to set a user-agent on the urllib request that will let the other site know that this is some kind of robot or proxy.


With that said, this sounds like a really stupid idea. What is your use case?

绅士风度i 2024-08-29 20:13:56

我只能谈论 django,但是...

如果您只想在多个网站上使用相同的对象/数据,您应该看看 django 站点框架

用于重定向我建议使用 重定向应用

或者您只需使用 重定向快捷方式 在您的视图中

i can only talk about django, but....

if you only want to use the same object/data on multiple websites you should have a look at the django sites framework

for redirects i would suggest the redirects app

or you simply use the redirect shortcut in your views

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