Urllib2: submit the form and then redirect

My goal is to come up with a portable urllib2 solution that will be a POST form, and then redirect the user to what comes out. The POSTing part is simple:

request = urllib2.Request('https://some.site/page', data=urllib.urlencode({'key':'value'}))
response = urllib2.urlopen(request)

The grant datasets the type of POST request. Now, I suspect that all the data that I should care about comes from response.info()and response.geturl(). I have to do self.redirect(response.geturl())inside the method get(self) webapp.RequestHandler.

But what should I do with the headlines? Anything else I forgot? Code snippets are much appreciated. :)

TIA.

EDIT: Here's a naive solution that I came across. Redirects, but the remote server shows an error indicating that it does not match the previous POSTed form:

info = response.info()
for key in info:
    self.response.headers[key] = info[key]
self.response.headers['Location'] = response.geturl()
self.response.set_status(302)
self.response.clear()
+1
3

, . , URL-, , - URL-, . - URL- , POSTed, GET, , POSTED. , , - POST URL-, - .

+2

urllib2 HTTPRedirectHandler. ( , ", ", , http, javascript - )

# Created handler
redirectionHandler = urllib2.HTTPRedirectHandler() 

# 2 apply the handler to an opener
opener = urllib2.build_opener(redirectionHandler)

# 3. Install the openers
urllib2.install_opener(opener)


request = urllib2.Request('https://some.site/page', data=urllib.urlencode({'key':'value'}))
response = urllib2.urlopen(request)

. urllib2.HTTPRedirectHandler.

+3

Source: https://habr.com/ru/post/1730368/


All Articles