CGI - HTML 到 python [英] CGI - HTML to python

查看:39
本文介绍了CGI - HTML 到 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试执行程序时,我正在遵循教程 CGI/p>

I am following the tutorial CGI, when I try to execute the program

#!C:/Python27/python.exe
# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"

HTML 文件

<form action = "/cgi-bin/hello_get.py" method = "get">
First Name: <input type = "text" name = "first_name">  <br />

Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>

在我提交值的浏览器中,我的 python 文件按原样显示.空白地我的代码被浏览器显示了.即使它没有得到名字和姓氏.有什么问题?

In my browser on submitting the values, my python file is displaying as it is. Blankly my code was shown by the browser. Even it didn't get the first name and last name. What is the problem?

我无法找到我在哪里做错了.

I am unable to find where I am doing the mistake.

推荐答案

如果我理解正确,您是说 Web 服务器正在显示 Python 程序的源代码,而不是执行源代码.根据您的 shebang 规范,您似乎在 Windows 下运行.根据您使用的 Web 服务器,例如 IIS 或 Apache,您需要配置 Web 服务器,以便它知道具有 .py 扩展名的文件将由 Python 解释器执行,因为它显然忽略了 shebang 规范.例如,如果使用 Apache,您的 httpd 配置文件可能包含以下几行:

If I understand correctly, you are saying that the web server is displaying the source of your Python program instead of executing the source. Based on your shebang specification, it appears you are running under Windows. Depending on what web server you are using, IIS or Apache for example, you need to configure the web server so that it knows that files with .py extensions are to be executed by the Python interpreter for it is clearly ignoring the shebang specification. For example, if using Apache your httpd configuration file might contain the following lines:

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

在第二行中,Web 服务器将忽略 shebang 并使用与文件类型 .py 关联的任何程序,它应该是您的 Python 解释器.这可能是您想要的,因为您可能正在升级您的解释器并更改其位置.在这种情况下,您可能应该将 shebang 编码为:

With the second line, the web server will ignore the shebang and use whatever program is associated with the filetype .py, which should be your Python interpreter. This is probably what you want as you may be upgrading your interpreter and its location changing. In this case you should probably code the shebang as:

#!/usr/bin/env python

这篇关于CGI - HTML 到 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆