HTTP POST and GET with cookies for authentication in python
You have to use the same "opener" you have created for all your requests, and it will handle the cookies all by itself.
here is an extract of something i wrote recently
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
# then for all requests
if postData:
pData = urllib.urlencode(postData)
else:
pData = None
httpReq = urllib2.Request(url, pData, self._headers)
page = opener.open(httpReq)
I would try using the requests
library. The documentation is excellent, and the code ends up being much cleaner than with urllib*
$ pip install requests
Using a session (see comment by Piotr) that handles cookies on its own, the result looks like this
import requests
url_0 = "http://webapp.pucrs.br/consulta/principal.jsp"
url = "https://webapp.pucrs.br/consulta/servlet/consulta.aluno.ValidaAluno"
data = {"pr1": "123456789", "pr2": "1234"}
s = requests.session()
s.get(url_0)
r = s.post(url, data)
It seems to work fine, as I get a "Usuario inexistente" notice for pr1
123456789 and "Sehna inválida" with your user-number.