1

我有个问题。我正在编写一个 python CGI 脚本,我必须在其中使用 html 输入来更改背景和前景色。我想实现cookie。我面临的问题是我可以更改任意数量的背景颜色,但是当我输入任何前景色时,我无法对输入进行任何更改,我必须再次重新启动应用程序。这是我的代码:

<pre>
<code>

    #!/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

<html>
  <head>
    <title>Biweekly Task 3</title>
  </head>
  """

if backgroundColor:

    print """
    <body bgColor=%s>
    <form method="GET" action="http://localhost:8000/cgi-bin/t3.py">
    </form>""" % (backgroundColor)


if foregroundColor:
    print """
    <body>
    <h1 style="color:%s"> Biweekly Task 3 </h1>
    <p style="color:%s"> Choose Background Color: <input type="text" name="background">
    <br/>
        Choose Foreground Color: <input type="text" name="foreground">
    <br/>
                   <input type="submit"></p>
    <form method="GET" action="http://localhost:8000/cgi-bin/t3.py">
    </form>""" % (foregroundColor, foregroundColor)


else:
    print """
    <form method="POST" >
    <h1> Biweekly Task 3 </h1>
      <p>Choose Background Color: <input type="text" name="background">
    <br/>
        Choose Foreground Color: <input type="text" name="foreground">
    <br/>
                   <input type="submit"  value="Change"></p>
    </form>
  </body>
</html>
"""
</code>
</pre>

谁能告诉我问题并修复此代码。

4

0 回答 0