milespowers
milespowers

Reputation: 11

Python urllib2 sending a post

I want to open a page then find a number and multiply by another random number and then submit it to the page So what I'm doing is saving the page as a html then finding the 2 numbers multiplying it then sending it as a post but

post = urllib.urlencode({'answer': goal, 'submit': 'Submit+Answer'})
req2 = urllib2.Request("example", None, headers)
response = urllib2.urlopen(req, post) #this causes it not to work it opens the page a second time

this makes it connect a second time and thus the random number sent is wrong since it makes a new random number so how can i send a post request to a page I already have open without reopening it?

Upvotes: 1

Views: 600

Answers (2)

Sam Starling
Sam Starling

Reputation: 5378

You might want to use something like mechanize, which enables stateful web browsing in Python. You could use it to load a URL, read a value from a page, perform the multiplication, place that number into a form on the page, and then submit it.

Does that sound like what you're trying to do? This page gives some information on how to fill in forms using mechanize.

Upvotes: 4

Gringo Suave
Gringo Suave

Reputation: 31860

I don't believe urllib supports keeping the connection open, as described here.

Looks like you'll have to send a reference to the original calculation back with your post. Or send the data back at the same time as the answer, so the server has some way of matching question with answer.

Upvotes: 0

Related Questions