Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - POSTing with a urllib2 opener

Tags:

python

urllib2

I have a urllib2 opener, and wish to use it for a POST request with some data.

I am looking to receive the content of the page that I am POSTing to, and also the URL of the page that is returned (I think this is just a 30x code; so something along those lines would be awesome!).

Think of this as the code:

anOpener = urllib2.build_opener(???,???)
anOpener.addheaders = [(???,???),(???,???),...,(???,???)]
# do some other stuff with the opener 
data = urllib.urlencode(dictionaryWithPostValues)
pageContent = anOpener.THE_ANSWER_TO_THIS_QUESTION
pageURL = anOpener.THE_SECOND_PART_OF_THIS_QUESTION
like image 340
Shariq Avatar asked Nov 19 '12 05:11

Shariq


2 Answers

This is such a silly question once one realizes the answer.

Just use:

open(URL,data)

for the first part, and like Rachel Sanders mentioned,

geturl()

for the second part.

I really can't figure out how the whole Request/opener thing works though; I couldn't find any nice documentation :/

like image 152
Shariq Avatar answered Oct 21 '22 20:10

Shariq


This page should help you out:

http://www.voidspace.org.uk/python/articles/urllib2.shtml#data

import urllib
import urllib2

url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
      'location' : 'Northampton',
      'language' : 'Python' }

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
the_url  = response.geturl() # <- doc claims this gets the redirected url

It looks like you can also use response.info() to get the Location header directly instead of using .geturl().

Hope that helps!

like image 38
Rachel Sanders Avatar answered Oct 21 '22 20:10

Rachel Sanders