我正在尝试运行以下程序:
#!/Python33/python
# Demonstrates get method with an XHTML form.
import urllib.parse
import cgi
import cgitb
import html
cgitb.enable()
def printHeader( title ):
print("Content-type: text/html; charset=utf-8")
print()
print("<html>")
print("<head><title>Test</title></head>")
print("<body>")
print(title)
print("</body>")
printHeader( "Using 'get' with forms" )
print ('''<p>Enter one of your favorite words here:<br /></p>
<form method = "get" action = "method.py">
<p><input type = "text" name = "word"/>
<input type = "submit" value = "Submit word"/>
</p>
</form>''')
pairs = cgi.parse();
if pairs.has_key("word"):
print ('''<p>Your word is:
<span style = "font-weight: bold">%s</span></p>''') \
% html.escape( pairs[ "word" ][ 0 ] )
print ("</body></html>")
print()
当我运行它时,我收到以下错误:
22 </p>
23 </form>''')
=> 24 pairs = cgi.parse();
25 if pairs.has_key("word"):
26 print ('''<p>Your word is:
pairs undefined, cgi = <module 'cgi' from 'C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\cgi.py'>, cgi.parse undefined
AttributeError: 'module' object has no attribute 'parse'
args = ("'module' object has no attribute 'parse'",)
with_traceback = <built-in method with_traceback of AttributeError object>
我使用的是Python 3.3,我找不到是否应该使用cgi.parse()
的替代品。
答案 0 :(得分:0)
cgi.parse()
exists in Python 3.3.你已经调用了一些其他文件“cgi.py”,Python正在寻找它。重命名该文件和任何相关的.py [co]文件。
答案 1 :(得分:0)
cgi.parse exists in python 3.3,但你有一个名为cgi.py的python文件,它正在找到它。
以下是命名错误的cgi.py文件所在的位置:
<module 'cgi' from 'C:\\Program Files (x86)\\Apache Software
Foundation\\Apache2.2\\htdocs\\cgi.py'>
python 3.3 does not have "has_key" though ...
所以你会在这一行上收到错误:
if pairs.has_key("word"):
将其更改为:
if "word" in pairs:
让代码正常运行。
输出:
Content-type: text/html; charset=utf-8
<html>
<head><title>Test</title></head>
<body>
Using 'get' with forms
</body>
<p>Enter one of your favorite words here:<br /></p>
<form method = "get" action = "method.py">
<p><input type = "text" name = "word"/>
<input type = "submit" value = "Submit word"/>
</p>
</form>
</body></html>