views:

350

answers:

3

My goal is to come up with a portable urllib2 solution that would POST a 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)

Providing data sets request type to POST. Now, what I suspect all the data I should care about comes from response.info() & response.geturl(). I should do a self.redirect(response.geturl()) inside a get(self) method of webapp.RequestHandler.

But what should I do with headers? Anything else I've overlooked? Code snippets are highly appreciated. :)

TIA.

EDIT: Here's a naive solution I came up with. Redirects but the remote server shows an error indicating that there's no match to the previously 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  A: 

I suspect this will almost always fail. When you POST a form, the URL you end up at is just the URL you posted to. Sending someone else to this URL, or even visiting it again with the same browser that just POSTed, is going to do a GET and the page won't have the form data that was POSTed. The only way this would work is if the site redirected after the POST to a URL containing some sort of session info.

Wooble
So, I POST something to an URL, the server sets a cookie in the response and doesn't redirect. Now, I set the very same cookie to client and redirect him to that URL. What do I get wrong?
alex
most (all?) browsers won't let your site set a cookie for another site, so this wouldn't work even if the site you're requesting from is doing some funky session handling instead of responding directly to a form post. I'm thinking that what you describe is simply impossible.
Wooble
A: 

The standard way to follow redirects with urllib2 is to use the HTTPRedirectHandler. (Not sure what you mean by 'what comes out' but I'm assuming a standard http redirect here, javascript redirect is a different beast)

# 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)

See urllib2.HTTPRedirectHandler for details on the handler.

monkut
So, the client browser 1)submits a form (POST) 2)gets redirected to some page (GET). I want do do 1 on the serverside and leave 2 to the client browser (like it's done 1 itself).
alex
A: 

You will find using mechanize much easier than using urllib2 directly

http://wwwsearch.sourceforge.net/mechanize/

gnibbler