简体   繁体   English

cgi脚本文件在浏览器中显示代码,而不是在运行

[英]cgi script file showing code in browser instead of running

My cgi script is : 我的cgi脚本是:

#!/usr/bin/env python

import cgi

reshtml='''Content-Type:text/html\n
<HTML>
<HEAD>
<title>
Friends CGI demo(dynamic screen)
</title>
</HEAD>
<body>
<h3>Friends list for:<i>%s</i></h3>
Your name is: <b>%s</b>
You have<b>%s</b>friends.
</body>
</HTML>'''

form = cgi.FieldStorage()
who = form['person'].value
howmany = form['howmany'].value
print(reshtml % (who, who, howmany))

here server returns full script as a text in place of executing it and according to me it should return only reshtml value which is understandable to browser. 在这里,服务器以文本形式返回完整脚本来代替执行它,根据我的看法,它应该仅返回可被浏览器理解的reshtml值。 I am using python web server and executing command C:\\Python32\\Lib\\http\\server.py successfully in current working directory. 我正在使用python Web服务器并在当前工作目录中成功执行命令C:\\ Python32 \\ Lib \\ http \\ server.py。 So what is the problem going on here? 那么这里发生了什么问题呢?

The default http server from that module does not include CGI support. 该模块的默认http服务器不包括CGI支持。 If you have Python3.4, perhaps you are missing the --cgi switch to http/server: 如果您拥有Python3.4,则可能缺少--cgi切换到http / server的方法:

c:\> python c:\python32\lib\http\server.py --cgi

or, more conveniently : 或者,更方便地:

c:\> python -mhttp.server --cgi

On the other hand, if you hgave Python3.2, try the following, which should work in both environments: 另一方面,如果您使用Python3.2,请尝试以下操作,该方法应在两种环境中均有效:

Create a file called httpd.py: 创建一个名为httpd.py的文件:

import http.server

server_address = ('',8000)
server_class = http.server.HTTPServer
handler_class = http.server.CGIHTTPRequestHandler
httpd = server_class(server_address, handler_class)
httpd.serve_forever()

Run it like so: 像这样运行它:

c:\> python \httpd.py

It will serve executable CGI files correctly, but only if they are located in ./cgi-bin/ or ./htbin/ . 它将正确提供可执行的CGI文件,但./cgi-bin/是它们位于./cgi-bin/./htbin/

Reference: 参考:

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

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