python / django中的Web代理? [英] Web proxy in python/django?

查看:133
本文介绍了python / django中的Web代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个代理作为获取图像的中介。一个例子是,我的服务器请求domain1.com/?url=domain2.com/image.png和domain1.com服务器将通过domain1.com服务器在domain2.com/image.png上回复数据。



本质上,我想将代理传递给我想要获取的URL,并让代理服务器响应该资源。



关于从哪里开始的任何建议?



我需要一些非常容易使用或实现的东西,因为我非常喜欢这一切的初学者。

我在python和/或django中找到的大多数解决方案都有代理作为翻译器,即domain1.com/image.png转换为domain2.com/image.png,其中显然不一样。



我目前有以下代码,但是获取图像会导致乱码数据:

  import httplib2 
from django.conf.urls.defaults import *
from django.http import HttpResponse

def proxy(request,url ):
conn = httplib2.Http()
如果request.method ==GET:
url = request.GET ['url']
resp,content = conn.request(url,request.method)
return HttpResponse(content)


解决方案

旧问题,但对于未来的Google员工,我认为这是你想要什么:

 #代理google标志
def test(request):
url = $ http://www.google.com/logos/classicplus.png
req = urllib2.Request(url)
response = urllib2.urlopen(req)
return HttpResponse(response.read (),mimetype =image / png)


I need to have a proxy that acts as an intermediary to fetch images. An example would be, my server requests domain1.com/?url=domain2.com/image.png and domain1.com server will respond with the data at domain2.com/image.png via domain1.com server.

Essentially I want to pass to the proxy the URL I want fetched, and have the proxy server respond with that resource.

Any suggestions on where to start on this?

I need something very easy to use or implement as I'm very much a beginner at all of this.

Most solutions I have found in python and/or django have the proxy acts as a "translater" i.e. domain1.com/image.png translates to domain2.com/image.png, which is obviously not the same.

I currently have the following code, but fetching images results in garbled data:

import httplib2
from django.conf.urls.defaults import *
from django.http import HttpResponse

def proxy(request, url):
    conn = httplib2.Http()
    if request.method == "GET":
        url = request.GET['url']
        resp, content = conn.request(url, request.method)
        return HttpResponse(content)

解决方案

Old question but for future googlers, I think this is what you want:

# proxies the google logo
def test(request):
    url = "http://www.google.com/logos/classicplus.png"
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    return HttpResponse(response.read(), mimetype="image/png")

这篇关于python / django中的Web代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆