djangourllib2来做http proxy,非常简单~~

xiaoxiao2021-02-28  81

from django.http import HttpResponse import urllib2 Prefix="http://www.abc.com/" opener = urllib2.build_opener(urllib2.ProxyHandler({'http':Prefix})) urllib2.install_opener(opener) def api(request): url = request.get_full_path() url = Prefix+url req = urllib2.Request(url, headers=request.META) conn = urllib2.urlopen(req) info = conn.info() data = conn.read() return HttpResponse(data, content_type=info.get("content-type"))

在view.py中实现如上,

在url.py中添加对应的路由即可:

 url(r'^$', api),

转载请注明原文地址: https://www.6miu.com/read-56262.html

最新回复(0)