开发者

How can I write a route/view/controller for a web framework which acts as a dumb proxy?

开发者 https://www.devze.com 2022-12-21 06:17 出处:网络
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 in

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/ru开发者_开发知识库by solution, but any will do, really.

Thanks in advance; alternate suggestions are welcome.


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>.*)$', 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 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号