如何使Python的urllib2遵循重定向并保持post方法 [英] How to make python urllib2 follow redirect and keep post method
问题描述
我使用的urllib2将数据发布到表单。问题是,在形式与302重定向应答。据 Python的HTT predirectHandler 重定向处理器将采取请求,并将其从POST转换成GET,并按照301或302,我想preserve POST方法,并传递到揭幕战中的数据。我通过简单地增加数据= req.get_data()到新的要求而作出的自定义HTT predirectHandler不成功的尝试。
我相信之前,所以我想我会做一个职位这项工作已经完成。
请注意:这类似于<一个href=\"http://stackoverflow.com/questions/554446/how-do-i-$p$pvent-pythons-urllib2-from-following-a-redirect\">this帖子和<一个href=\"http://stackoverflow.com/questions/110498/is-there-an-easy-way-to-request-a-url-in-python-and-not-follow-redirects/110808\">this 之一,但我不想prevent重定向我只是想继续POST数据。
下面是我的HTT predirectHandler不起作用
类MyHTT predirectHandler(urllib2.HTT predirectHandler):
高清redirect_request(个体经营,REQ,FP,code,味精,头,NEWURL):
响应重定向返回请求或无。 这是由http_error_30x方法调用时
重定向响应被接收。如果应该重定向
发生,返回一个新的请求,允许http_error_30x到
执行重定向。否则,引发HTTPError如果没有人
否则应尽量处理这个网址。返回None如果你不能
但另一个处理程序可能。
M = req.get_method()
如果(code在(301,302,303,307)和M(GET,HEAD)
或code在(301,302,303)和m ==POST):
#严格(根据RFC 2616),301或302响应
#一个POST不得造成无确认重定向
#来自用户(urllib2的的,在这种情况下)。在实践中,
#基本上所有的客户都在这种情况下重定向,所以我们
# 照着做。
#包含空格的URI conciliant
NEWURL = newurl.replace('','%20')
返回请求(NEWURL,
标题= req.headers,
数据= req.get_data()
origin_req_host = req.get_origin_req_host(),
不能证实= TRUE)
其他:
引发HTTPError(req.get_full_url(),code,味精,头,FP)
这实际上是一个非常糟糕的事情我想过这个问题就越多。举例来说,如果我提交表单
http://example.com/add (用POST数据增加一个项)
和响应是一个302重定向到 http://example.com/add 以及我张贴我贴我第一次相同的数据将无限循环告终。不知道为什么我之前没有想到这一点。我将离开这里的问题只是作为一个警告,任何人想这样做。
I am using urllib2 to post data to a form. The problem is that the form replies with a 302 redirect. According to Python HTTPRedirectHandler the redirect handler will take the request and convert it from POST to GET and follow the 301 or 302. I would like to preserve the POST method and the data passed to the opener. I made an unsuccessful attempt at a custom HTTPRedirectHandler by simply adding data=req.get_data() to the new Request.
I am sure this has been done before so I thought I would make a post.
Note: this is similar to this post and this one but I don't want to prevent the redirect I just want to keep the POST data.
Here is my HTTPRedirectHandler that does not work
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)
This is actually a really bad thing to do the more I thought about it. For instance, if I submit a form to http://example.com/add (with post data to add a item) and the response is a 302 redirect to http://example.com/add and I post the same data that I posted the first time I will end up in an infinite loop. Not sure why I didn't think of this before. I'll leave the question here just as a warning to anyone else thinking about doing this.
这篇关于如何使Python的urllib2遵循重定向并保持post方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!