title: Logging in to University website using Python
body: <p>I'm trying to build a python script that will automatically open my University website and log me in. The site uses CAS for authentication, and I am using the requests module to pass in my credentials to the login form. No matter what I try I cannot get past the login screen.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import requests, webbrowser, sys
  
  with requests.Session() as s:
      url = 'https://signin.bradley.edu/cas/login?service=https://mybu.bradley.edu/MyBU/Login'
  
      USERNAME = ''
      PASSWORD = ''
  
      response = s.get(url)
      content = str(response.content)
      LT = content[2558:2599]
      EXECUTION = content[2660:2666]
      ID = content[2726:2734]
  
      print(LT)
      print(EXECUTION)
      print(ID)
  
      login_headers = {'User-Agent':'Mozilla/5.0',
                      'Referer':"https://signin.bradley.edu/cas/login?service=https://mybu.bradley.edu/MyBU/Login"}
  
      payload = {'username':USERNAME, 
                  'password':PASSWORD,
                  'lt':LT,
                  'execution':EXECUTION,
                  '_eventId':ID}
  
  
      response = s.post(url, data=payload, headers = login_headers)
  
      print(response.status_code)
      print(response.content)
  </code></pre>
  
  <p>I use string slicing to extract the hidden form values needed to add to the POST message payload(LT token, Execution, _eventId). The three print statements in the middle of the script body are for debugging purposes.</p>
  
  <p>Here is one instance of output from the script:</p>
  
  <pre><code>"LT-55040-XwgXJsgy2bpnc59lGKTJutrS7amrrG"
  "e1s1"
  "submit"
  500
  b'\n\n&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;\n\n\n\n\n\n\n\n\n&lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en"&gt;\n\t&lt;head&gt;\n\t    &lt;title&gt;Bradley University Signin Service&lt;/title&gt;\n        \n\t\t\n        &lt;link type="text/css" rel="stylesheet" href="/cas/css/cas.css" /&gt;\n        &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;\n\t    &lt;link rel="icon" href="/cas/favicon.ico" type="image/x-icon" /&gt;\n\t&lt;/head&gt;\n\t&lt;body id="cas" class="fl-theme-iphone"&gt;\n    &lt;div class="flc-screenNavigator-view-container"&gt;\n        &lt;div class="fl-screenNavigator-view"&gt;\n            &lt;div id="header" class="flc-screenNavigator-navbar fl-navbar fl-table"&gt;\n\t\t\t\t\n                &lt;h1 id="app-name" class="fl-table-cell"&gt;Bradley University Signin Service&lt;/h1&gt;\n            &lt;/div&gt;\t\t\n            &lt;div id="content" class="fl-screenNavigator-scroll-container"&gt;\n\t\t&lt;div id="welcome"&gt;\n\t\t\t&lt;h2&gt;CAS is Unavailable&lt;/h2&gt;\n\n\t\t\t&lt;p&gt;\n\t\t\t   There was an error trying to complete your request.  Please notify your support desk or try again.\n\t\t\t&lt;/p&gt;\n\t\t&lt;/div&gt;\n\n\n\n&lt;/div&gt;\n                &lt;div id="footer" class="fl-panel fl-note fl-bevel-white fl-font-size-80"&gt;\n                \n                \n\n &lt;p&gt;Served by snape  &lt;/p&gt;\n \n                \t&lt;a id="jasig" href="http://www.jasig.org" title="go to Jasig home page"&gt;&lt;/a&gt;\n                    &lt;div id="copyright"&gt;\n                        &lt;p&gt;Copyright &amp;copy; 2005 - 2012 Jasig, Inc. All rights reserved.&lt;/p&gt;\n                        &lt;p&gt;Powered by &lt;a href="http://www.jasig.org/cas"&gt;Jasig Central Authentication Service 3.5.2.1&lt;/a&gt;&lt;/p&gt;\n                    &lt;/div&gt;\n                &lt;/div&gt;\n            &lt;/div&gt;\n        &lt;/div&gt;\n        &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;\n        &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js"&gt;&lt;/script&gt;\n        &lt;script type="text/javascript" src="/cas/js/cas.js"&gt;&lt;/script&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n\n'
  </code></pre>
  
  <p>You can see that the hidden values I mentioned above are contained in the output of the <code>print(response.content)</code> statement.</p>
  
  <p>Thanks for any help!</p>