简体   繁体   English

使用Python-CGI创建HTML表单时出错

[英]Error in creating HTML form using Python-CGI

I have to create a HTML form and get the data using python-cgi. 我必须创建一个HTML表单并使用python-cgi获取数据。 HTML form requires user to submit firstname and lastname and then the python script is supposed to get the data generated in the form. HTML表单要求用户提交名字和姓氏,然后python脚本应该获取在表单中生成的数据。 I have read up tutorials and tried playing with it to make it work, but it does not seem to happen. 我已经阅读了教程,并尝试使用它来使其工作,但似乎没有发生。 HTML code: HTML代码:

<form method = "POST" action ="http://localhost/cgi-bin/pyfile_test.py">
    <p>First Name: <input type="text" name="firstname">
    <p>Last Name: <input type="text" name="lastname">
    <p>Click here to submit the form:<input type="submit" value="Submit">
    <input type="hidden" name="session" value="1898fd">
    </form>

python script to extract data python脚本提取数据

#!c:/Python26/python.exe -u

import cgi, cgitb
cgitb.enable()

def main()
    print "Content-type: text/html\n"
    print
    form = cgi.FieldStorage()
    if form.has_key("firstname") amd form["firstname"].value !="":
    print "<h1>Hello", form["firstname"].value, "</h1>"
    else:
    print "<h1> Error!Wrong!</h1>
main()

when i direct html form to above script it does NOT work but when i direct it to the following script it works. 当我将html表单定向到上面的脚本时,它不起作用,但是当我将其定向到以下脚本时,它起作用。

#!c:/Python26/python.exe -u

import cgi, cgitb
cgitb.enable()

print "Content-type: text/html"
print

print "The First Python Script On Server Side"
print "The sum of two numbers evaluation"
print "3+5 = ",3+5

I dont know what is going wrong. 我不知道怎么了。 Any help will be highly appreciated! 任何帮助将不胜感激!

At least in what you just showed, there are syntax errors: 至少在您刚刚显示的内容中,存在语法错误:

if form.has_key("firstname") amd form["firstname"].value !="":
print "<h1>Hello", form["firstname"].value, "</h1>"
else:
print "<h1> Error!Wrong!</h1>

amd should be and , and both print statements should be indented deeper than the if and else keywords that control them. amd应该是and ,并且两个print语句的缩进应比控制它们的ifelse关键字深。

If you fix your syntax and try again, what exactly do you see in the browser (and what do you see if you use your browser's "show source" functionality)? 如果您修复了语法然后再试一次,您在浏览器中会看到什么(以及如果使用浏览器的“显示源”功能会看到什么)?

Edit : 编辑

One more syntax error: you're missing a closing " on the second print . It's really tiresome to spot all your syntax errors one by one, and Apache (or whatever your web server is) doesn't seem to be helping -- cgitb can't take control until the whole script is successfully parsed, so syntax errors are the one reason it can't show! 还有一个语法错误:你缺少一个右"在第二print 。这真是烦人一个地发现所有的语法错误之一,阿帕奇(或任何Web服务器是)似乎并不被帮助- cgitb在成功解析整个脚本之前无法控制,因此语法错误是无法显示的原因之一!

And one more: missing colon at the end of the def main line. 还有一个:在def main末尾缺少冒号。

Why don't you just run it with python yourscript.py at the command line until you've made syntax-error-free, and only then try again to run it as a CGI? 您为什么python yourscript.py在命令行使用python yourscript.py运行它,直到您实现无语法错误, 然后再次尝试将其作为CGI运行?

Eg after fixing the two errors I spotted earlier but not the two I've just spotted, you'd be seeing (if python is on the PATH and the shell prompt is $ ): 例如,在修复了我先前发现的两个错误但没有发现我刚刚发现的两个错误之后,您会看到(如果python在PATH上,而shell提示符是$ ):

$ python acgi.py 
  File "acgi.py", line 6
    def main()
             ^
SyntaxError: invalid syntax

because of the missing " at the end of the previous line and missing colon at line 6. 因为在上一行的末尾缺少" ,而在第6行缺少了冒号。

Fixing the last two errors (total of four in the code you originally posted) you finally see: 修复最后两个错误(最初发布的代码中总共四个),您最终会看到:

$ python acgi.py 
Content-type: text/html


<h1> Error!Wrong!</h1>

of course it says "wrong" because the field storage is empty, but this shows you can NOW (with all four errors fixed) sensibly try to run it as a CGI script at last;-) 当然,它说“错误”,因为字段存储为空,但这表明您现在可以(在修复所有四个错误的情况下)明智地尝试最后将其作为CGI脚本运行;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM