title: Python CGI Cookies
body: <p>I have a question. I am coding a python CGI script in which i have to change the background and foreground colors using html input. I am suppose to implement cookies. The problem i am facing is that i can change as many background colors as i want, but when i enter any foreground color, i cannot do any more changes with the inputs and i have to restart the application again. Here is my code:</p>
  
  <pre><code>&lt;pre&gt;
  &lt;code&gt;
  
      #!/usr/bin/python
  
  import cgi
  import cgitb
  cgitb.enable()
  
  import Cookie
  import os
  
  backgroundColor = None
  foregroundColor = None
  
  form = cgi.FieldStorage();
  if 'background' in form:
      C = Cookie.SimpleCookie()
      backgroundColor = form.getvalue('background')
      C['background'] = backgroundColor
      print C
  
  if 'foreground' in form:
      D = Cookie.SimpleCookie()
      foregroundColor = form.getvalue('foreground')
      D['foreground']= foregroundColor
      print D
  
  
  
  print """Content-Type: text/html
  
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;Biweekly Task 3&lt;/title&gt;
    &lt;/head&gt;
    """
  
  if backgroundColor:
  
      print """
      &lt;body bgColor=%s&gt;
      &lt;form method="GET" action="http://localhost:8000/cgi-bin/t3.py"&gt;
      &lt;/form&gt;""" % (backgroundColor)
  
  
  if foregroundColor:
      print """
      &lt;body&gt;
      &lt;h1 style="color:%s"&gt; Biweekly Task 3 &lt;/h1&gt;
      &lt;p style="color:%s"&gt; Choose Background Color: &lt;input type="text" name="background"&gt;
      &lt;br/&gt;
          Choose Foreground Color: &lt;input type="text" name="foreground"&gt;
      &lt;br/&gt;
                     &lt;input type="submit"&gt;&lt;/p&gt;
      &lt;form method="GET" action="http://localhost:8000/cgi-bin/t3.py"&gt;
      &lt;/form&gt;""" % (foregroundColor, foregroundColor)
  
  
  else:
      print """
      &lt;form method="POST" &gt;
      &lt;h1&gt; Biweekly Task 3 &lt;/h1&gt;
        &lt;p&gt;Choose Background Color: &lt;input type="text" name="background"&gt;
      &lt;br/&gt;
          Choose Foreground Color: &lt;input type="text" name="foreground"&gt;
      &lt;br/&gt;
                     &lt;input type="submit"  value="Change"&gt;&lt;/p&gt;
      &lt;/form&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  """
  &lt;/code&gt;
  &lt;/pre&gt;
  </code></pre>
  
  <p>Can anyone tell me the problem and fix this code.</p>