0

I am trying to run a simple Python script from my lighttpd server. The HTML code is:

<html>
<title>Interactive page</title>
<body>
    <form method=POST action="cgi-bin/cgi101.py">
        <P><B>Entery your name: </B>
        <P><input type=text name=user>
        <P><input type=submit>
    </form>
</body>
</html>

My Python script is:

#!/usr/bin/python
import cgi
form = cgi.FieldStorage()
print('Content type:text/html \n')
print('<title>Reply Page</title>')
if not 'user' in form:
    print('<h1>who are you</h1>')
else:
    print(cgi.escape(form['user'].value))

So my question is, am I able to load the HTML page and then I click on Submit Query? Firefox is asking "You have chosen to open cgi101.py" from localhost and asking what should Firefox do with this file and whether I want to save it. Shouldn't it just open in Firefox and run the Python script rather than asking me to save the Python script?

4

2 回答 2

1

昨天我在学习 Python 编程书时遇到了同样的问题。之后我发现答案是你丢失了一个重要的步骤。那是你丢失了一个 webserver.py 文件,它在 Python 中实现了一个 HTTP Web 服务器,它知道如何运行用 Python 编码的服务器端 CGI 脚本;从当前工作目录提供文件和脚本。这是代码:

`import os,sys
from http.server import HTTPServer,CGIHTTPRequestHandler

webdir = '.'
port = 80

os.chdir(webdir)
srvraddr = ("",port)
srvrobj = HTTPServer(srvraddr,CGIHTTPRequestHandler)
srvrobj.serve_forever()`

你可以再试一次!

于 2013-12-22T03:20:21.653 回答
0

找到解决方案添加 chmod +x 文件名以使其可执行。

于 2013-05-06T08:33:49.170 回答