title: Python urllib simple login script
body: <p>I am trying to make a script to login into my "check card balance" service for my university using python. Basically it's a web form where we fill-in our PIN and PASS and it shows us how much $$$ is left on our card (for food)...</p>
  
  <p>This is the webpage: [url]http://www.wcu.edu/11407.asp[/url]</p>
  
  <p>This is the form I am filling:</p>
  
  <pre><code>&lt;FORM method=post action=https://itapp.wcu.edu/BanAuthRedirector/Default.aspx&gt;&lt;INPUT value=https://cf.wcu.edu/busafrs/catcard/idsearch.cfm type=hidden name=wcuirs_uri&gt; 
  &lt;P&gt;&lt;B&gt;WCU ID Number&lt;BR&gt;&lt;/B&gt;&lt;INPUT maxLength=12 size=12 type=password name=id&gt; &lt;/P&gt;
  &lt;P&gt;&lt;B&gt;PIN&lt;BR&gt;&lt;/B&gt;&lt;INPUT maxLength=20 type=password name=PIN&gt; &lt;/P&gt;
  &lt;P&gt;&lt;/P&gt;
  &lt;P&gt;&lt;INPUT value="Request Access" type=submit name=submit&gt; &lt;/P&gt;
  &lt;/FORM&gt;
  </code></pre>
  
  <p>As you will see the fields i need to fill in are:</p>
  
  <pre><code>&lt;input maxlength="12" size="12" type="password" name="id"&gt;
  &lt;input maxlength="20" type="password" name="PIN"&gt;
  </code></pre>
  
  <p>then I need to press the button:</p>
  
  <pre><code>&lt;input value="Request Access" type="submit" name="submit"&gt;
  </code></pre>
  
  <p>When i do this in my browser, this takes me to another page where it shows my balance and id in simple html...</p>
  
  <p>So I tried to write a python script that would give the the html of that page (so I could parse out the amount of money I have left and print it to the screen)</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>import urllib                                                                   
  import urllib2                                                                  
  import sys                                                                      
  import cookielib                                                                
  import hashlib                                                                  
  
  cookieJar = cookielib.LWPCookieJar()                                            
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))           
  
  opener.addheaders = [('User-agent', "Mozilla/5.0")]                             
  
  username = "xxxxxxx"                                                          
  password = "xxxxxxxx"                                                             
  action   = "https://itapp.wcu.edu/BanAuthRedirector/Default.aspx"        
  
  print hashlib.md5(hashlib.md5(password).hexdigest()).hexdigest()                
  #url = "http://www.wcu.edu/11407.asp"                                           
  url = "http://www.wcu.edu/11407.asp"                                            
  form = {"action" : action,                                                      
          "id" : username,                                                        
          "PIN" : password}                                                       
  
  encodedForm = urllib.urlencode(form)                                            
  request = urllib2.Request(url, encodedForm)                                     
  page = opener.open(request)                                                     
  contents = page.read()                                                          
  
  f = open("mycatpage.txt", "w")                                                  
  f.write(contents)                                                               
  f.close()
  </code></pre>
  
  <p>Why does't this work???</p>
  
  <p>Thanks in advance</p>
  
  <p><strong>EDIT</strong>
  I made a new version of the script, but it gives me an error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "checkbalance3.py", line 20, in &lt;module&gt;
      open("mycatpage.html", 'w').write(opener.open(request))
    File "/usr/lib/python2.7/urllib2.py", line 400, in open
      response = meth(req, response)
    File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
      'http', request, response, code, msg, hdrs)
    File "/usr/lib/python2.7/urllib2.py", line 438, in error
      return self._call_chain(*args)
    File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
      result = func(*args)
    File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
      raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
  urllib2.HTTPError: HTTP Error 500: Internal Server Error
  </code></pre>
  
  <p>This is the code:</p>
  
  <pre><code>from urllib import urlopen, urlencode                                           
  import urllib2                                                                  
  
  myId = 'xxxxxxxx'                                                              
  myPin = 'xxxxxxx'                                                                
  
  data = {                                                                        
              'id':myId,                                                          
              'PIN':myPin,                                                        
              'submit':'Request Access',                                          
              'wcuirs_uri':'https://cf.wcu.edu/busafrs/catcard/idsearch.cfm'      
          }                                                                       
  
  opener = urllib2.build_opener()                                                 
  opener.addheaders = [('User-agent','Mozilla/5.0')]                              
  
  url = 'https://itapp.wcu.edu/BanAuthRedirector/Default.aspx'                    
  request = urllib2.Request(url, urlencode(data))                                 
  
  open("mycatpage.html", 'w').write(opener.open(request))
  </code></pre>
  
  <p>Any ideas?</p>