如何使python urllib2遵循重定向并保持post方法

时间:2009-08-11 04:06:53

标签: python automation urllib2

我正在使用urllib2将数据发布到表单。问题是表单回复302重定向。根据{{​​3}},重定向处理程序将接受请求并将其从POST转换为GET并遵循301或302.我想保留POST方法和传递给开启者的数据。通过简单地将data = req.get_data()添加到新请求,我在自定义HTTPRedirectHandler上尝试失败。

我确信此前已经完成,所以我想我会发帖。

注意:这与Python HTTPRedirectHandlerthis post类似,但我不想阻止重定向,我只想保留POST数据。

这是我的HTTPRedirectHandler不起作用

class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
    """Return a Request or None in response to a redirect.

    This is called by the http_error_30x methods when a
    redirection response is received.  If a redirection should
    take place, return a new Request to allow http_error_30x to
    perform the redirect.  Otherwise, raise HTTPError if no-one
    else should try to handle this url.  Return None if you can't
    but another Handler might.
    """
    m = req.get_method()
    if (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
        or code in (301, 302, 303) and m == "POST"):
        # Strictly (according to RFC 2616), 301 or 302 in response
        # to a POST MUST NOT cause a redirection without confirmation
        # from the user (of urllib2, in this case).  In practice,
        # essentially all clients do redirect in this case, so we
        # do the same.
        # be conciliant with URIs containing a space
        newurl = newurl.replace(' ', '%20')
        return Request(newurl,
                       headers=req.headers,
                       data=req.get_data(),
                       origin_req_host=req.get_origin_req_host(),
                       unverifiable=True)
    else:
        raise HTTPError(req.get_full_url(), code, msg, headers, fp)

1 个答案:

答案 0 :(得分:6)

这实际上是一件非常糟糕的事情,我想的就越多。例如,如果我提交表格 http://example.com/add(包含要添加项目的帖子数据) 并且响应是302重定向到http://example.com/add并且我发布了第一次我将在无限循环中结束时发布的相同数据。不知道为什么我之前没想过这个。我会在这里留下这个问题,只是为了警告其他人在考虑这样做。