如何让python urllib2跟随重定向并保持POST方法

2 投票
1 回答
7824 浏览
提问于 2025-04-15 13:31

我正在使用urllib2库向一个表单发送数据。问题是这个表单会返回一个302重定向。根据Python的HTTPRedirectHandler,重定向处理器会把请求从POST转换成GET,然后跟随301或302的重定向。我希望能够保留POST方法和传递给打开器的数据。我尝试自定义一个HTTPRedirectHandler,简单地在新的请求中添加data=req.get_data(),但没有成功。

我相信之前有人遇到过这个问题,所以我想发个帖子来讨论一下。

注意:这和这个帖子以及那个帖子类似,但我并不想阻止重定向,我只是想保留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 个回答

6

其实,这样做是个很糟糕的主意,越想越觉得不对。例如,如果我向 http://example.com/add 提交一个表单(里面有添加项目的数据),然后服务器给我一个302重定向,还是指向 http://example.com/add,如果我再提交一次之前的数据,就会陷入一个无限循环。真不知道我之前怎么没想到这一点。我把这个问题留在这里,主要是想提醒其他有类似想法的人。

撰写回答