title: HTTP POST and GET with cookies for authentication in python
body: <p>I'm trying to create a python program that logs in to my university's site using my id and password. This is the formal page for logging in: <a href="https://webapp.pucrs.br/consulta/" rel="noreferrer">https://webapp.pucrs.br/consulta/</a></p>
  
  <p>As you may notice, the two fields are named pr1 and pr2. The page uses POST to send the data. ALSO, there's a cookie that is downloaded when the page is loaded, it's a JSESSIONID containing a random value that, as I understood, you have to return on the header of the POST method to authenticate the login.</p>
  
  <p>I wrote the following code, but the return page on the GET method says "The session was not initialized", probably cause the cookie was not sent back properly.</p>
  
  <pre><code>from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
  import httplib, urllib, cookielib, Cookie, os
  
  conn = httplib.HTTPConnection('webapp.pucrs.br')
  
  #COOKIE FINDER
  cj = cookielib.CookieJar()
  opener = build_opener(HTTPCookieProcessor(cj),HTTPHandler())
  req = Request('http://webapp.pucrs.br/consulta/principal.jsp')
  f = opener.open(req)
  html = f.read()
  for cookie in cj:
      c = cookie
  #FIM COOKIE FINDER
  
  params = urllib.urlencode ({'pr1':111049631, 'pr2':&lt;pass&gt;})
  headers = {"Content-type":"text/html",
             "Set-Cookie" : "JSESSIONID=70E78D6970373C07A81302C7CF800349"}
              # I couldn't set the value automaticaly here, the cookie object can't be converted to string, so I change this value on every session to the new cookie's value. Any solutions?
  
  conn.request ("POST", "/consulta/servlet/consulta.aluno.ValidaAluno",params, headers) # Validation page
  resp = conn.getresponse()
  
  temp = conn.request("GET","/consulta/servlet/consulta.aluno.Publicacoes") # desired content page
  resp = conn.getresponse()
  
  print resp.read()
  </code></pre>
  
  <p>Where do I put this cookie so the login is authenticated?</p>