我使用的urllib2将数据发布到表单。 的问题是,在形式与302重定向应答。 根据Python的化HTTPRedirectHandler重定向处理器将采取的请求,并将其从POST转换成GET,并按照301或302,我想保留POST方法,并传递到揭幕战中的数据。 我通过简单地增加数据= req.get_data()到新的要求而作出的一个HTTPRedirectHandler的定制不成功的尝试。
我相信之前,所以我想我会做一个职位这项工作已经完成。
注:此类似, 这篇文章和这一个 ,但我不想阻止重定向我只是想继续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)