1

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?

  • HTTP protocol is stateless. Each HTTP GET and HTTP POST is a separate request. I believe you have failure in your design / thinking. If you want stateful protocol you need to use cookies and sessions. – Mikko Ohtamaa Aug 22 '11 at 20:05
  • i have cookies to keep me logged in i didn't post the whole script – milespowers Aug 22 '11 at 23:26

2 Answers2

4

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.

Sam Starling
  • 5,298
  • 3
  • 35
  • 52
0

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.

Community
  • 1
  • 1
Gringo Suave
  • 29,931
  • 6
  • 88
  • 75