Django:如何返回原始响应

发布于 2024-12-13 01:15:59 字数 717 浏览 4 评论 0原文

这就是我想要的。

  1. 向外部站点提交POST请求(即登录信息)。
  2. 接收响应
  3. 将原始响应返回到我的客户端浏览器(包含用于登录的 cookie
    验证)。
  4. 如果客户尝试在新选项卡中访问该网站,他会发现他已经登录。

我成功完成了步骤 1 和 2。 2(提交 POST 并收到站点的响应)。

request = urllib2.Request(url, formData, headers)
response = urllib2.urlopen(request)

但是当我尝试在视图中返回它时,

return response

我收到以下错误

Django Version:     1.3.1
Exception Type:     AttributeError
Exception Value:    addinfourl instance has no attribute 'has_header'
Exception Location:D:\Python27\lib\site-packages\django\utils\cache.py in patch_vary_headers

注释: 我以前遇到过 csrf 错误,但我使用装饰器 @csrf_exempt & 禁用了 csrf错误消失了

This is what I want.

  1. Submit a POST request to a external site(i.e login information).
  2. Receive the response
  3. Return the raw response to my client's browser(containing the cookies for login
    validation).
  4. If the client tries to access the site in new tab he finds that he is already signed in.

I successfully completed steps 1 & 2 (submitted POST & received the response from the site).

request = urllib2.Request(url, formData, headers)
response = urllib2.urlopen(request)

But when I try to return it in the view

return response

I get the folllowing error

Django Version:     1.3.1
Exception Type:     AttributeError
Exception Value:    addinfourl instance has no attribute 'has_header'
Exception Location:D:\Python27\lib\site-packages\django\utils\cache.py in patch_vary_headers

note:
I had a csrf error previosly,but i disabled csrf using decorator @csrf_exempt & the error was gone

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

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

发布评论

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

评论(2

自我难过 2024-12-20 01:15:59

您不应直接从 urlopen 方法返回响应。相反,您的视图应该返回 django 的 HttpResponse,其中正文和标头应设置为原始响应中的内容:

from django.http import HttpResponse
import urllib2

def my_view(request):
    request = urllib2.Request(url, formData, headers)
    response = urllib2.urlopen(request)

    # set the body
    r = HttpResponse(response.read())

    # set the headers
    for header in response.info().keys():
        r[header] = response.info()[header]

    return r

You shouldn't return the response from urlopen method directly. Instead your view should return an instance of django's HttpResponse, where body and the headers should be set to those from the original response:

from django.http import HttpResponse
import urllib2

def my_view(request):
    request = urllib2.Request(url, formData, headers)
    response = urllib2.urlopen(request)

    # set the body
    r = HttpResponse(response.read())

    # set the headers
    for header in response.info().keys():
        r[header] = response.info()[header]

    return r
瑶笙 2024-12-20 01:15:59

首先,在视图中提交 URL 请求作为响应是没有意义的。但是,即使你可以,那也不会达到你想要的效果。没有办法让用户登录到不同的站点,原因很明显:这将是一个巨大的安全漏洞。

Firstly, it doesn't make sense to submit a URL request as the response in a view. But, even if you could, that wouldn't do what you want. There's no way to log a user in to a different site, for what should be obvious reasons: that would be a massive security hole.

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