title: Page Redirect in CGI using Python
body: <p>I am trying to write a cgi script in python for a simple login page.
  I am trying to redirect this page to my home page on submit of username and password.</p>
  
  <p>Can anyone help me to do this without manipulating the HTTP Headers.</p>
  
  <h2>This is the code that i am using::</h2>
  
  <pre><code>    #!"D:\Python34\python.exe"
  
      import cgi
      username=""
      password=""
  
      def htmlTop():
          print("""Content-type:text/html\n\n
                   &lt;!DOCTYPE html&gt;
                   &lt;html lang="en"&gt;
                      &lt;head&gt;
                          &lt;meta charset="utf-8"/&gt;
                          &lt;title&gt;Login Form&lt;/title&gt;
                          &lt;link rel="stylesheet" href="../css/bootstrap.min.css"&gt;
                          &lt;link rel="stylesheet" href="../css/bootstrap-theme.min.css"&gt;
                          &lt;link rel="stylesheet" href="../css/userStyles.css"&gt;
                      &lt;/head&gt;
                      &lt;/body&gt;""")
      def htmlBody():
          print("""
                  &lt;div class = "container"&gt;
                      &lt;div class="wrapper"&gt;
                          &lt;form action="" method="get" name="Login_Form" class="form-signin"&gt;       
                              &lt;h3 class="form-signin-heading"&gt;Welcome Back! Please Sign In&lt;/h3&gt;
                                &lt;hr class="colorgraph"&gt;&lt;br&gt;
  
                                &lt;input type="text" class="form-control" name="Username" placeholder="Username" required="" autofocus="" /&gt;
                                &lt;input type="password" class="form-control" name="Password" placeholder="Password" required=""/&gt;            
  
                                &lt;button class="btn btn-lg btn-primary btn-block"  name="Submit" value="Login" type="Submit"&gt;Login&lt;/button&gt;            
                          &lt;/form&gt;         
                      &lt;/div&gt;
                  &lt;/div&gt;
                  """)
      def checkForm():
          formData=cgi.FieldStorage()
          if formData.getvalue("Submit"):
              username=formData.getvalue("Username")
              password=formData.getvalue("Password")
              print("Your Username is {0} and your password is {1}".format(username,password))
          else:
              htmlBody()
      def htmlTail():
          print("""   
                      &lt;script src="../js/bootstrap.min.js"&gt;&lt;/script&gt;
                      &lt;/body&gt;
                   &lt;/html&gt;""")
  
      #main program
      if __name__=="__main__":
          try:
                  htmlTop()
                  checkForm()
                  htmlTail()
          except:
              print ("This is the exception")
              cgi.print_exception()
  </code></pre>
  
  <hr>