urllib2を使用してデータをフォームに投稿しています。問題は、フォームが302リダイレクトで応答することです。 Python HTTPRedirectHandlerによれば、リダイレクトハンドラはリクエストを受け取り、それをPOSTからGETに変換し、301または302に従います。私はPOSTメソッドとオープナに渡されたデータを保存したいと思います。私は単純に、新しいRequestにdata = req.get_data()を追加するだけで、カスタムHTTPRedirectHandlerの試行に失敗しました。python urllib2をリダイレクトしてポストメソッドを保持する方法
私はこれが前に行われたと確信しているので、私は投稿をすると思った。
注:これはthis postおよびthis oneと似ていますが、私はリダイレクトを防ぎたくないので、POSTデータを保持したいだけです。
は、ここでこれは実際に私はそれについて考え、より行うには本当に悪いことである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)
あなたは公共のインターネットで作業しているフォームですか?私は何が起こっているかを診断する方法についていくつかの考えを持っていますが、まだ適切な答えはありません。私は、しかし、調査したいと思います。 –